This is a discussion on random numbers adding up.. within the C++ Programming forums, part of the General Programming Boards category; hello! I've made my first random number generator, and everytime i try it out, the numbers are random, but they ...

hello!
I've made my first random number generator, and everytime i try it out, the numbers are random, but they all add up. so if i have a randomness range between 1 and 52 (card game) it's randomness is like this:

3, 12, 18, 25, 30, 32, 41, 49

and that is not really random i think :P

this is the source code

Code:
```#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;

int randomRange (int lowest, int highest) {
return rand() % (highest - lowest) +lowest;  //random number between the lowest and highest,
}                                           //including the highest (see, +lowest)

int main () {
srand(time(NULL));
cout << randomRange(1,52);
}```
anyone knows whats up?

2. If you can reproduce this same behaviour multiple times, your compiler's implementation of rand() is not very good.
C++11 allows you to choose the exact algorithm for generating random numbers, via the <random> header file.

3. When i do a for loop, going trough this random generating code 1000 times it is generated randomly, but everytime i stárt the program, it's usually a little more than the last time i ran the program

4. Not sure what the issue is. The code basically generates a random number between 1 and 52. If you want the random generator to display the random number n only once (so that the numbers 1-52 do not get repeated), you must add more logic to perform that type of behavior. How about some ouput of your console and additional details on what you want the code to do.

5. You might check time(NULL) just to make sure the function is working correctly.
It is not likely to be wrong; but, it is worth checking because it is easier to check.

Tim S.

6. O_o

Let me guess, you are executing the program with little to no time between runs.

Soma

7. Originally Posted by phantomotap
O_o

Let me guess, you are executing the program with little to no time between runs.

Soma
Wouldn't that yield the same or similar series' ?..and not ones in which every number is greater than its predecessor ?

8. Wouldn't that yield the same or similar series' ?..and not ones in which every number is greater than its predecessor ?
You are, for some reason, seeing two things that exhibit similar behavior and screaming "These are caused by different things!".

The default assumption should be that things having similar symptoms are caused by the same underlying condition until further evidence is obtained.

If he were printing out longer runs of random samples the series itself would be similar or slightly showing a mutation related to the underlying implementation.

He is only printing out a single random sample.

The only value he prints is obviously going to fall victim to the same underlying condition that causes a series to exhibit repeated sameness or slight mutation.

You may look at it like this to confirm the problem for yourself because you seem to understand the seed related to time: he is printing a series that has exactly one element.

None of this, by the way, suggests anything about the quality of the underlying implementation.

If a "PRNG" didn't produce the same sequence with the same seed it would be all but useless as a "PRNG".

Using the current time as a seed without allowing time for the seed to change is the problem. It naturally depends on the implementation of the "PRNG" and how time is handled on the host system but the problem is generally related to how many samples must be processed before the least significant changes to a seed propagates from the "avalanche" effect.

As a matter of interest, if you sample one value of random data from a known seed (say 12345) and thoroughly mix the seed obtained from the time you can generally make the least significant bits, the parts of time that are most likely to change, more significant causing faster "avalanche" effects.
[/Edit]

Soma

9. First of all, that does not generate numbers in the correct range.
The range calculation requires a +1:
Code:
`rand() % (highest - lowest + 1) + lowest`
I would also add an assert that highest >= lowest since you wouldn't want to divide by zero, or worse, would you?

The next problem is with how you are running the program. I'm guessing that you're scripting it in something like a batch file and due to that the seed upon each run is almost the same. Don't worry about this. If you write a real program, i.e. something that doesn't exit within a millisecond, then it will not be a problem.
(A better PRNG wont help much if the problem is the way it is being seeded)

Make sure that in your real program, you put the srand call near the top of main and only ever call it once.

Lastly, if you really want accurate unbiased results, then you need to learn about the rejection method as per post 6 here. And maybe, just maybe, you'll also need a better PRNG.

10. Originally Posted by phantomotap
he is printing a series that has exactly one element.
Now I see...
I thought the code in the OP was just a demo and each run of the real program (card game, as mentioned ) produced a sequence of numbers showing the same trend.
That was a quite stupid assumption.. :S

11. That was a quite stupid assumption..
^_^;

You assumed that a newbie was using `srand', `time', and `rand' correctly?

Wow. That is a stupid assumption. ^_~

Soma

12. I am a noob, i admit, but i am trying hard
I just love coding so much :P
But seeing this, i thought of something. i understand that you can get the seed by the srand(time(NULL)); , but by putting those #include <cstdlib> and #include <ctime> i don't need to make the function rand() right? or do you need to write it somewhere at the start of a program as a prototype or something?

13. Originally Posted by Reinmaster7
I am a noob, i admit, but i am trying hard
I just love coding so much :P
But seeing this, i thought of something. i understand that you can get the seed by the srand(time(NULL)); , but by putting those #include <cstdlib> and #include <ctime> i don't need to make the function rand() right? or do you need to write it somewhere at the start of a program as a prototype or something?
rand() is defined in <cstdlib> and time() is defined in <ctime>, if you include those headers you can use them right away by just calling them.

Also, i would advice against ever writing your own implementation of a PRNG and then calling it "rand()".

14. Originally Posted by Neo1
i would advice against ever writing your own implementation of a PRNG and then calling it "rand()".
I don't understand this.
i dont entirelyunderstand the rand statement, but i do think to know that this is a function that takes the socalled seed from the srand function, and makes calculation with it to generate random numbers. i dont write my own impementation of a PRNG, i dont even know what that is xD