Rand isnt random enough, please help!

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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  // 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.
Thanks in advance!

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:

1
2
3
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.

2) This is bad advice
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <random> // 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<int> 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.
<snip - misread the post>
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.