Hello, I want to generate random numbers in range of -4 and 4
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
int main()
{
srand(time(0));
int result=0;
for(int i=0;i<1000;i++)
{
int r=(rand()%8)-4; // number between -4 and 4
result+=r;
}
cout <<result<<endl;
return 0;
}
This doesn't seem to work correctly because the final result is not close to 0. I tried changing int r=(rand()%8)-4; to int r=(rand()%9)-4; but I don't get a correct result.
If I generate generate a lot of numbers between n and -n, their sum must get close to 0. Am I right?
Why don't you print out the numbers as they come? ( to include +4 in the resulting numbers you need to use the %9 )
rand() is not a good random number generator so you won't get the resulting sequence really well distributed
If P and Q are independent random variables distributed uniformly over the same range [ min...max ], then
the random variable P+Q is distributed normally.
rand()%8 returns values in the range 0...7, so
(rand()%8) - 4 returns values in the range -4...3.
The mean is ( -4 + 3 ) / 2 = -0.5. Summing 1000 such numbers gives an expected value of -500.
Running your program 10000 times, the average of the 10000 runs was -499.401.
About this random function/class. Anyone has written their own version of random function/class that is *supposedly* to give truly random numbers? Can you share? :P
I feel the library provided random function if we call them repeatedly enough times, the pattern repeats and this can be bad from user perspective. Of cuz in reality, user maybe too occupied to notice the random-ness is actually not random at all :P
C's rng -- rand() -- has a period of 2^32, meaning that it repeats after 4 billion+ numbers generated. C's rng always
provides the same stream of numbers, regardless of what it was seeded with. All the seed does is determine where
in that stream it should start.
You can look at the boost libraries -- they have a number of random number generators. ISAAC is another that boost
does not have. All of these generators have periods much, much larger than 2^32 (indeed some are more in the
range of 2^8000 and even higher).
The "randomness" of an algorithm can't really be quantified. "randomness" is essentially the property that one cannot
distinguish a pattern, not necessarily that there isn't a pattern. True randomness is the property that there is no pattern.
However we can't in general prove that, therefore we can never say that something is truly random.
There are many, many statistical tests available that test for "randomness".
Some people say that the encryption of communication between home computers is compromised, because all seed from the machine's clock and the latter is generally guessable (within a range). I am not very intimate with IT security, so I don't know if this can be exploited with contemporary communication protocols. Some companies sell external hardware for random number generation. They are supposed to be physically sourced, but I have never used them personally.