Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

IronMan9675

Random number generating...

This topic is 6180 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement

...
#include
...

my_function()
{
srand(time(NULL)); //seed the randomizer with the clock

...

//for a number say 1-6 of a dice roll
roll = (rand() % 6) + 1;
}

hope that helps.

MojoMonkey

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
If you want N-1 to be the largest random number and 0 the smallest use: rand() % N. Don''t forget to call randomize().

Share this post


Link to post
Share on other sites
Ok this is just some words of wisdom from me bccause im bored
"did u know that computer carnt genrate random numbers!! they are psudo random numbers because nothing calculated can be random"
isnt that nice


~prevail by daring to fail~

Share this post


Link to post
Share on other sites
exactly! That''s why you seed the randomizing function with the clock. This will give you a different value everytime you try it. Unless time stops....

Share this post


Link to post
Share on other sites
Actually nothing is random even in real life. Scientist have taken interest in the randomizity of lava lamps, but even their variation depends on the conditions. If there was a big bang and there will be a big crunch the explosion will depend on the conditions of the energy and our universe might actually be repeating the same pattern over and over or it might change every time depending on the big crunch, think of dividing two figures, pii for example...

Here's a "randomizer" function I use :
  

#include <math.h>
#include <limits.h>


int rmi(int min,int max)
{
static unsigned seed = 0;
int figure;
time_t sekond;
if (seed==0)
{
time(&sekond);
seed=(unsigned) ((sekond % UINT_MAX)+1);
srand(seed);
}
figure = rand();
figure = min + figure % (max - min + 1);
return(figure);
}


Edited by - Afterlife on August 8, 2001 5:03:03 PM

Edited by - Afterlife on August 8, 2001 5:05:43 PM

Edited by - Afterlife on August 8, 2001 5:06:50 PM

Edited by - Afterlife on August 8, 2001 5:07:31 PM

Edited by - Afterlife on August 8, 2001 5:08:14 PM

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Okay obviously none of you knows anything about the behavior of subatomic particles. Secondly using the modulus operator in attempting to set the upper bound on a "random" number greatly decreases the distribution associated with how "random" a number is in relation to prior calls to rand(). This may sound odd but its true.

Suppose you had a perfect random number generator, say p_rand(), that generated each integer from [0, RAND_MAX] equally as often. If you wanted a random number from [0, n) then you would execute p_rand()%n. From our original assumption we know p_rand() has a perfect distribution in [0, RAND_MAX] but does p_rand()%n have even a good distribution in [0, n)? A less than perfect distribution would create some numbers more often than others making your results hardly random.

Let''s look at the % operator. A call to p_rand()%2 would map each even integer to 0 and each odd integer to 1. If RAND_MAX is even then there are (RAND_MAX/2)+1 even integers and RAND_MAX/2 odd integers. The call to p_rand()%2 generates a 0 slightly more often than a 1. Well no problem you say just make RAND_MAX odd. 0 and 1 now occur at the same frequency but what if p_rand()%3 is executed? If RAND_MAX is not multiple of three we will have the same problem as before: certain integers will occur more often than others, throwing off our perfect random number generator. Obviously RAND_MAX can''t be changed for each % on each p_rand() call (its #define''d in stdlib.h).

As most of you pointed out there are no real random number generators on computers so p_rand() is just a fantasy. Programmers attempt to provide the best approximations possible and in these approximations some integers occur more often than others. This magnifies the problem of using % even more when trying to generate random numbers. Putting the approximation function rand() together with an unknown RAND_MAX and the % operator you can get some pretty unrandom results. Try running the following program on your platform:

#include
#include
#include
#include

int main()
{
srand((unsigned)time(0));
long even = 0, odd = 0;

for (long i = 0; i < LONG_MAX; ++i) // replace LONG_MAX if you don''t want to wait awhile
if (rand()%2 == 0) ++even;
else ++odd;

cout << "Even: " << (double)even/LONG_MAX << ''%'' << endl;
cout << "Odd: " << (double)odd/LONG_MAX << ''%'' << endl;

return 0;
}

For some applications--games--even a 45/55 split is unacceptable. Ever throw your controller at the TV yelling "This game is cheating!"?

If you want a good random number generator do a yahoo search for Blitz++. Its a good number crunching set of classes for math, physics, and even games. I''ve used it a few times in some AI programs with great success.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Make that:

#include <iostream>
#include <limits>
#include <cstdlib>
#include <time>

Share this post


Link to post
Share on other sites
yes, and if you want to look behind the scenes try searching for "Mersenne Twister", it''s a random number generator which, if properly implemented, is ~4 times faster than good ol'' rand() .

Share this post


Link to post
Share on other sites
Even using srand() will not produce a fair distribution of randomly generated numbers!

I refer you to ''Numerical Recipes in C'', it gave me some lovely random number generators for my neural nets!

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!