Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

random number between x and y

Status
Not open for further replies.

lordhuh

Programmer
Apr 25, 2000
96
US
i have been trying to get a way to get a psudorandom number between 2 numbers. rand() returns an integer between 1 and 0x7fff i have no idea what that converts to so its is really difficult to get a number from it. this is for my final project in c++ class and its due today. PLEASE HELP!!!! Karl Pietri
 
You have to include <time.h>
then
...
srand(time(NULL));
int ran = rand()%0x7fff + 1; // this will give you a random number between 1 and 7fff Best Regards,

aphrodita@mail.krovatka.ru {uiuc rules}
 
srand(time(NULL));
int ran = rand()%0x7fff + 1; // this will give you
//a random number between 1 and 8000 :)

Correct is:
srand(time(NULL));
int ran = rand()%(0x7fff - 1) + 1;
//or
int ran = rand()%0x7ffg + 1;
// this will give you
//a random number between 1 and 7fff
John Fill

ivfmd@mail.md
 
Well, if you started correcting such little things ...
Taking this from your post:

//or
int ran = rand()%0x7ffg + 1;
// this will give you
//a random number between 1 and 7fff

That hardly can get you anything, it must be e instead.
Best Regards,

aphrodita@mail.krovatka.ru {uiuc rules}
 
ok let me clarify this. i wanted to know how to get a number between x and y.
Code:
int num=rand();
will return a number between 1 and 0x7fff. msdn says that exact thing in the rand help. but it dosnt matter now i found some code on and all i have to do now is random_number(min,max)
but thanks for the quidkness of your replies Karl Pietri
 
As I think is some thing like following?
int rnd(x,y)
{
return rand()%(y - x) + x;
} John Fill
1c.bmp


ivfmd@mail.md
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top