and let's say i have a range from 2 to N, what should i do to get the output only from 2 to N and not 0 to N?because what's given in the previous thread is a function to choose from 0 to N.
thank you :D

Say you want a random number from 2 to n where n is 15, you would do the following:

``````int n = 15;
int random_int;

random_int = (rand()%n-2)+2;``````

lets look at the min/max scenarios.

MIN:
Should be 2 (right?)
so you random 0. But you said +2 at the end. so you get 2. which is minimum.

MAX:
should be 15. But max of (15-2) is 13. But you added 2. So its 15.

Problem solved

frtzfoxy -1

OKAY YOU NOOBS!!!!!
HERE IS A MUCH SIMPLER THING TO DO!!!!

``````#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
int randomNumber = rand() % 100 + 1; // This would randomize the number from 1-100
cout << "The Random Number Is: " << randomNumber << endl;
system("pause");
return 0;
}
/*
If you want to randomize from 1-10
then put: 10 + 1
If you want to randomize from 1-9999
then put: 9999 + 1
SIMPLE!!!
*/
``````
NathanOliver commented: You dont need to ressurect a dead thread to add nothing new. check out the first post and you will se he has this same bit of code except its for 1-10 +0
Lucaci Andrew commented: As NathanOliver said, plus he wrote it 8 years ago. /sight -1
deceptikon 1,790

OKAY YOU NOOBS!!!!!
HERE IS A MUCH SIMPLER THING TO DO!!!!

It's also subtly broken if you care about uniform distribution. Simple isn't always better...noob. ;)

deceptikon, thanks for the link ! now i know how to use rand() correctly :D

you can do it easily just by only rand function
this is link which clearly explains how to generate random number with example.it will really help you.
http://softwares-mart.blogspot.com/2012/12/how-to-generate-random-number-in-c.html

Thanks for the feedback about the font size on my web site. I wasn't aware that anyone had a problem with it till now. I'll look into it

Thanks, Bob! Expecially love the last line, the Conclusion. rand() is great! But for small programs. I use some complicated external library and rand() is not working.

Hello (if anyone is here),
I was looking through the previous discussion messages and saw someone had put #include <conio> to use the getch() but I'm using DEV-C++ and it says there is no such file.. Is that something that is in Visual C++? If I'm trying to loop the program if "y" for y/n choice, how would I do that using the while fucntion?
Thank you, Megan

NathanOliver 429

using standard code you could do the following

``````char choice;
do
{
std::cout << "Do you want to go again (y/n): ";
std::cin.get(choice);
} while (::tolower(choice) == 'y');
``````

Thank you Bob. This is the first time i figure out a random number.

Bob!! Thank you bro! The tutorial was really helpful! learned alot from this.
--Amateur

Yo!
can you show us how to print random numbers between -1 to +1?
thanks

David W 131

Hey @ Zain_4 ...

Do you realize that it is not proper etiquette to hijack a thread?

and if you need programming help with an homework question ...

you will NEED TO FIRSTLY show us the code that you have tried so far ...

how would i make something akin to a dice roll game. for example: the player would roll and it would generate a random number (perhaps from (1-60) and then the program would generate a random number for itself, acting as a simple AI (im really new to c++ and i want to learn this stuff)

Reverend Jim 4,899

Well, first I would learn how to program in C++. What I would not do is ask someone else to do it without showing that I had put some effort into doing it myself first.

Ty_1 0

I have a question to ask. It is that i trying to make a program that using do-while loop but the random number seems like doesn't change after the first one. It keep continue appears the same number. How can I make it random everytime the loop. Thanks.

JamesCherrill 4,733

Ty_1

1. Do not hijack old topics for your new question. Start you own new topic.
2. If you do not post your code then nobody can tell you what you are doing wrong!
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.