Im trying to make a program that will randomly pick a number between 1 and 1million, but it just isnt random enough and I cannot get an output which even reaches the hundred thousand mark (ie, all outputs have been less than 100,000)

Ive even tried running the output through a second RNG but end up with similar results. Any tips please?

 ``12345678910111213141516171819`` `````` // Rune Randomiser is intended to select a rune at random, based on their individual drop rates. #include "C:\Users\leezeebub\Documents\Visual Studio 2010\Projects\std.lib.facilities" int main () { int seed2, seed, runenum; cout << "please mash the number pad!\n"; cin >> seed; srand ( seed ); seed2 = rand() % 1000000 + 1; srand ( seed2 ); runenum = rand() % 1000000 + 1; cout << runenum << " \n"; keep_window_open(); return 0; }``````

I realize my code is a little messy, but thats just where ive been editing it so much. I will tidy it up when I have something that works.

EDIT: Also, how can I loop this program? It would make testing much quicker if i didnt have to restart the program everytime :)
Last edited on
`srand (time (NULL) );` for a random seed (current system time)
The rand() funtion returns a random number between 0 and RAND_MAX, which defines an integer value that is 32,767 or larger.

that means, rand() by itself can't reach 100,000.

i had this idea a few months ago, why not add together multiple calls of rand() function.
to generate a number between 0 and 1m, and if RAND_MAX is 32,767.
you'll need to add about 31 calls of rand() together, you gotta watch though, you shouldn't MULTIPLY one call of rand() by 31, because this will affect randomness.
you could try this:

 ``123`` ``````int rnd = 0; for(int i = 0; i<31 ; i++) rnd += rand();``````

then use the value of rnd.
hope that helped.
I tried using time but that also isnt very random. If used multiple times in quick succession, the output only changes by a few numbers.

first output may be 2045
then the second may be 2098
then the third may be 2138
etc

And as this program is most likely to be used at around 7pm on a weekly basis we are going to end up with a very predictable result.
Thanks Rechard3, I didnt know that there was a cap on it. I shall experiment with this and see what I can get.
Well `rand()` is a pseudo-random number generator after all, but it still runs on a very complex algorithm, if you want something even more random look up:
http://www.cplusplus.com/reference/random/random_device/

EDIT:
As further posts by people who know better than me point out:
1) This information is partially false
MiiNiPaa wrote:
std::rand() usually implemented as linear congruental generator. Which is anything but complex.

JLBorges wrote:
Also, a non-deterministic random devices may not be in a suitable state in which it can generate random numbers

Sorry
Last edited on
Use the C++11 random number library. For instance:

 ``123456789101112131415`` ``````#include #include // http://en.cppreference.com/w/cpp/numeric/random int main() { for( int i = 0 ; i < 10 ; ++i ) { int seed ; std::cout << "seed? " ; std::cin >> seed ; std::seed_seq seq( { seed, seed*3+1, seed+101, seed*7+57, seed*11+201 } ) ; std::mt19937 gen(seq) ; std::uniform_int_distribution dist( 1, 1000000 ) ; for( int j = 0 ; j < 10 ; ++j ) std::cout << dist(gen) << ' ' ; std::cout << '\n' ; } }``````

http://ideone.com/MQ1Auf
Last edited on
Ah ha, ok thanks guys! I need to go eat something but these look promising.
Thanks a lot!
Vidminas wrote:
but it still runs on a very complex algorithm
std::rand() usually implemented as linear congruental generator. Which is anything but complex.
Last edited on
> if you want something even more random look up:
> http://www.cplusplus.com/reference/random/random_device/

It need not be 'even more random' than `std::minstd_rand`
 If implementation limitations prevent generating non-deterministic random numbers, the implementation may employ a random number engine. - IS

Also, a non-deterministic random devices may not be in a suitable state in which it can generate random numbers (for instance, it may not have harvested enough entropy) and the constructor or `operator()()` after it is constructed may throw. In general, this advice (from the page linked to) is sound:
 Unless the program really requires a stochastic process to generate random numbers, a portable program is encouraged to use an alternate pseudo-random number generator engine instead, or at least provide a recovery method for such exceptions.

Topic archived. No new replies allowed.