Skip to main content
GameDev.net gamedev.net
🔒 Locked

Random Numbers

Started by NetherDaemon Jan 24, 2003 at 9:59 AM 14 replies 550+ views
Original Post
NetherDaemon
NetherDaemon
I''ve been having some trouble getting the bloody rand() function working in my program. I have all the included, but it still thinks its an undeclared identifier when i try something like i = rand()%6. PLEASE HELP!!!
Kuladus
Kuladus
are u working in the correct namespace?
jenova
jenova
make sure to include the correct header.

#include &ltstdlib.h>

NOTE: a hint to VC users. if you type the name of the function and search the help. you'll notice the help lets you know which header to include and which library to link when using the specified function. you can also goto MSDN and do the same thing.

To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.

[edited by - jenova on January 24, 2003 11:29:18 AM]
To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.
Vich
Vich
I once made a function to get a random integer within a certain range. Thought it might be helpfull.


int random(int max, int min)
{
float randomnr = rand();
randomnr = randomnr / RAND_MAX;
return int( ((max - min) * randomnr) + min );
}

void main()
{
// randomize
srand((unsigned)time(NULL));
// generate a random number between 1 and 10
int i = random(1,10);
}
[www.LifeIsDigital.net - My open source projects and articles.
darkchrono4
darkchrono4
quote:
Original post by Vich
I once made a function to get a random integer within a certain range. Thought it might be helpfull.


int random(int max, int min)
{
float randomnr = rand();
randomnr = randomnr / RAND_MAX;
return int( ((max - min) * randomnr) + min );
}

void main()
{
// randomize
srand((unsigned)time(NULL));
// generate a random number between 1 and 10
int i = random(1,10);
}



I tried this in VC++ and I keep getting the same random numbers. Any other simplish way to get random numbers?

eldee
eldee

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


int RandInt(int min, int max)
{
if(min == max)
return max;

return rand()%(max - min) + min;
}

int main()
{
for(int i = 0; i < 10; i++)
cout << "Random number between 1 and 10: " << RandInt(1, 10) << endl;
return 0;
}


output:

Random number between 1 and 10: 6
Random number between 1 and 10: 9
Random number between 1 and 10: 8
Random number between 1 and 10: 5
Random number between 1 and 10: 9
Random number between 1 and 10: 2
Random number between 1 and 10: 4
Random number between 1 and 10: 1
Random number between 1 and 10: 8
Random number between 1 and 10: 3
Press any key to continue


[edit- damn code tags...]

-eldee
;another space monkey;
[ Forced Evolution Studios ]

::evolve::

Do NOT let Dr. Mario touch your genitals. He is not a real doctor!


[edited by - eldee on January 28, 2003 12:52:32 AM]

darkchrono4
darkchrono4
OK, if I use that I get a random number everytime. But I call it three times in a row and get the same number.
Naku
Naku
You have to generate a random seed to get different numbers each time you run the program if you''re using rand().

If you call : srand(timeGetTime()); at the start of the program that should give you different numbers each time.

You''ll have to #include <mmsystem.h> and link winmm.lib to use timeGetTime().
darkchrono4
darkchrono4
Insane, I was seeding the srand() in the function that generated the random number and got the same one every time. Seeded it in WinMain() and it works great. What gives with that one?
daerid
daerid
You could do something like this:


  
int Random(int lower,int upper)
{
static bool seeded = false;
if(!seeded)
{
srand(time(0));
seeded = true;
}
float rnd = (float)rand();
rnd /= RAND_MAX;
return int(((upper - lower) * rnd) + lower);
}
daerid@gmail.com
darkchrono4
darkchrono4
I got my random numbers just by moving when I seed it at. My question is if I was seeding the generator with the system time everytime I called the function why was it coming up with the same number?
Extrarius
Extrarius
Because you were seeding it to the same thing each time. The time function returns seconds (I think) and you were calling rand several times a second. Each time you called it (since it was in the same second) it ended up with the same seed.

Also, if you want a really good pseudo-random number generator (depending on the compiler, the rand function might only return 16 bits, and it might not have a large period), you can use the Mersenne Twister(
http://www.math.keio.ac.jp/matumoto/emt.html) which is supposedly just as fast as rand and its one of the best pseudo-random number generators.

[edited by - Extrarius on January 29, 2003 3:08:36 PM]
"Walk not the trodden path, for it has borne it's burden." -John, Flying Monk
darkchrono4
darkchrono4
I guess if I had stopped to think about it for a second I could have figured it out that it was using the same seed. Thanks for the enlightenment though.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.