Generate a random number question

W

Wahoo

Another question,my teacher gave me a code for

generate a random number from 1 - range, but I can't made it work, where is
the problem?

Thanks!!





code:

#include <math.h>

unsigned int RandomNumber(int range) {

static int seed = 1;

srand(seed);

seed++;

return (rand() % range) + 1;

}


~ Let us linux ~
 
K

Karl Heinz Buchegger

Wahoo said:
Another question,my teacher gave me a code for

generate a random number from 1 - range, but I can't made it work, where is
the problem?

Define: can't made it work

The only thing that can be seen immediatly, is:
Don't seed the random number generator more then once.
In other words: call srand only once in your program. Typically
this is done in main, at the very beginning.

int main()
{
srand( time( 0 ) );

...

}

If you seed the random number generator yourself all the
time, you are actually doing harm to the quality of the
generator. 'Randomness' is a statistical property of a *sequence*
of numbers. You need to let the generator work on it's own in order
to build up an undisturbed (quasi-)random sequence.
 
J

John Harrison

Wahoo said:
Another question,my teacher gave me a code for

generate a random number from 1 - range, but I can't made it work, where is
the problem?

Thanks!!

code:

#include <math.h>

unsigned int RandomNumber(int range) {

static int seed = 1;

srand(seed);

seed++;

return (rand() % range) + 1;

}

You need a better teacher, that function is rubbish.

The C FAQ has a section on generating random numbers.

http://www.eskimo.com/~scs/C-faq/s13.html questions 13.15 to 13.20. 13.16
and 13.17 explain why your teachers function is poor.

john
 
B

Bill Seurer

Here are some things to think about.
#include <math.h>

rand() and srand() aren't in math.h header but in stdlib.h. This is
probably why the code doesn't even compile for you. Also check up on
the "new" way of including the C header files (cmath and cstdlib).
unsigned int RandomNumber(int range) {

Hmmm. Just what does it mean to return an unsigned number but have a
signed range? What SHOULD happen if "range" is 0? Or if it is negative?
static int seed = 1;
srand(seed);
seed++;

You set the seed for the generator every time you enter the function to
one more than what it was set to the previous time. Why? You might
want to read about what seeds are used for.
return (rand() % range) + 1;

See above and think about what will happen for 0 or negative values for
"range".
 
J

Jerry Coffin

[ ... ]
The C FAQ has a section on generating random numbers.

http://www.eskimo.com/~scs/C-faq/s13.html questions 13.15 to 13.20. 13.16
and 13.17 explain why your teachers function is poor.

The "solutions" presented in the FAQ provide little improvement.

If rand() is at all competently written, several of the least
significant bits have already been eliminated, so there's little real
difference between using % and using / to clamp the output to a range.

Unfortunately, both result in biased results except when RAND_MAX
happens to be an even multiple of the range you're asking for. In the
common case that RAND_MAX is a prime number, that's a pretty rare
occurrence...

Code like this can produce unbiased results:

int rand_lim(int limit) {
/* return a random number between 0 and limit inclusive.
*/

int divisor = RAND_MAX/(limit+1);
int retval;

do {
retval = rand() / divisor;
} while (retval > limit);

return retval;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top