Solved

# Random Numbers... argghhhhh!

Posted on 2004-09-03
251 Views
This is driving me insane!

I need a function that I can call that will provide me with a random integer from within a range... the function might be something like int randomNumber(int UpperBound);

help?

Thanks

MrBarby
0
Question by:MrBarby
• 7
• 5
• 4

LVL 11

Expert Comment

ID: 11976062

rand() % UpperBound

This is the poor man's version of what you want.

-bcl
0

LVL 11

Accepted Solution

ID: 11976086
So, you could write a function to do what the above says:

int randomOnRange(int n) {
return rand() % n;
}

returns a number on the assymetric range [0-n)

The reason it is the poor man's version is that some implementations of rand() are not very random in their low-order bits. You probably don't care.

-bcl

0

LVL 86

Assisted Solution

jkr earned 100 total points
ID: 11976127
Actually, you can increase the 'randomness' by using

int randomOnRange(int n) {

srand( (unsigned)time( NULL ) );

return rand() % n;
}
0

Author Comment

ID: 11976146
the main problem I have with rand() at the moment is the fact that the first time it is called it is the same every time... is there a way to stop this?
0

LVL 11

Expert Comment

ID: 11976158
jkr's seeding with the time will start the sequence in a new spot.
0

Author Comment

ID: 11976161
sorry, jkr, didnt see you comment there - i think that might work, but since I will be calling this many times in a loop, time will not be suitable... is there something else I can use to set the seed?
0

LVL 86

Expert Comment

ID: 11976164
Use 'srand()', as I wrote above - it initializes the randum number generator with a new value. E.g.

/* RAND.C: This program seeds the random-number generator
* with the time, then displays 10 random integers.
*/

#include <stdlib.h>
#include <stdio.h>
#include <time.h>

void main( void )
{
int i;

/* Seed the random-number generator with current time so that
* the numbers will be different every time we run.
*/
srand( (unsigned)time( NULL ) );

/* Display 10 numbers. */
for( i = 0;   i < 10;i++ )
printf( "  %6d\n", rand() );
}
0

LVL 86

Expert Comment

ID: 11976179
You could call it once before entering the loop. Or (but, that's exotic)

int randomOnRange(int n) {

srand( (unsigned)time( NULL ) * rand ());

return rand() % n;
}
0

LVL 11

Expert Comment

ID: 11976181
Again:

srand((unsigned)time(NULL));

will seed the pseudo-random number generator so that it starts at a different place in the sequence. The sequence itself is fixed but very long so you don't have to worry about it.

You really want to seed the value once (jkr: Why would I want to seed the value every time? That makes little sense to me but I am happy to learn) at the beginning of the program execution.

-bcl
0

LVL 11

Expert Comment

ID: 11976192
If I were going to do this I would use a static flag:

int randomOnRange(int n) {
static int init = 1;
if (init) {
srand((unsigned)time( NULL ));
init = 0;
}
return rand() % n;
}

-bcl
0

LVL 86

Expert Comment

ID: 11976207
>> Why would I want to seed the value every time?

That would increase the randomness a little bit, but in fact is counterproductive when called in short intervals.
0

LVL 11

Expert Comment

ID: 11976234
jkr-
Sorry to hijack another's question but how, exactly, would reseeding the random number generator with a series of values from an increasing sequence increase the randomness of the sequence? Can you point me at a reference? I glance through Knuth and I am not convinced. I also don't spend much of my time thinking about randomness so I stand ready to be amazed by this working.

-bcl
0

Author Comment

ID: 11976260
Hmm... so are u supposed to set the seed just once or once every call?  Setting it every call produces the same number every time lol.
0

LVL 11

Expert Comment

ID: 11976298
Seeding everytime at a very fast interval will keep resetting the seed to the same value. That makes sense (if your loop is short enough and fast enough).

Periodically reseeding it should NOT increase the randomness according to my brain and quick reading right now. I remain open to being completely out of my mind.

-bcl
0

Author Comment

ID: 11976368
cheers, thanks for your help :)
0

LVL 86

Expert Comment

ID: 11978645
>>but how, exactly, would reseeding the random number generator with a series of values from an
>>increasing sequence increase the randomness of the sequence?

Not the randomness of the sequence that is initialized, but the exchange of sequences by using a different initializer is the idea. That will not lead to really random numbers, but by changing the initial vaue periodicaqlly, the numbers' will be more evenly distributed (IMHO, but fact is that my statistics readings date back by over 12 years :o)
0

## Featured Post

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their waâ€¦
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the bâ€¦
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.