I am new at coding, and I wanted to make a little game using the random number generator. Below you see the way I learned to make a random number generator. The problem is, that this is not as random as you would have thought. The number keeps increasing with the same amount everytime I run the compiler (however it depends on how long I wait when I run it again). I can basically guess what the "random" number is a lot of the time. I know it uses time, but it adds like 10 to the random number for every second. So why can't it be more random? And is there a way to make it more random?
And is there another way to make random numbers than this too?
Given the weight of a die, it's orientation and it's initial velocity the out come of any dice roll can be calculated before it even lands. If you record the order in which the cards in a deck are stacked and then serialize all of the interactions with that deck then you will know the order in which they will appear. My point is OP, nothing is truly random; so how close does this solution need to be?
Some implementation of rand() returns current state before advancing it. That means that first returned value will be seed it was initialized with. Do warm-up of your RNG by doing something like:
It is really predictable. I tried to run it 4 times and the outcome was
2. 5. 12. 15. 19 etc. (increasing by a small amount every second) I ran it about once every second. Shouldn't it be more like 55. 23. 67. 34 etc.? Shouldn't the algorithm for the rand() function not be more complex than increasing the value by 5 each second or so? (Try to run it yourselves, do you get the same result?)
If you go to the installation directory for MingW and enter mingw32-C++.exe --version what GCC version does it say you are using? The release notes for GCC 4.8 mention the addition of hardware support to the RNG. I'm reaching here of course, this is a really weird issue and I'm not sure where to start.
I found the directory, but I couldn't find the .exe file. But I think the version is. 4.7.1 as you can see on this screenshot I took: http://imgur.com/lJrWI9C
Have you tried the code and seen if it works with your compiler? :)
It does, you have to go to the installation directory from the command shell though. The binary mingw32-C++.exe is in "%INSTALLED_DIRECTORY%\Mingw\bin".
Ok, so I just updated it with the link you gave me. Using the command shell is really confusing because i've never used it before. However I got the same output as Chervil ^^, when I used his code.. Really weird.
What IDE/Compiler do you use? Because in my computer the mingw32-c++.exe is under "C:\Program Files (x86)\CodeBlocks\MinGW\bin".
Yes, I forgot to add that under normal circumstances it is not necessary to call srand() more than once. The only reason I did so was to simulate the effect of re-running a program repeatedly within a short timescale.
True random number generator
A random number generator that produces non-deterministic random numbers, if supported.
Unlike the other standard generators, this is not meant to be an engine that generates pseudo-random numbers, but a generator based on stochastic processes to generate a sequence of uniformly distributed random numbers. Although, certain library implementations may lack the ability to produce such numbers and employ a random number engine to generate pseudo-random values instead. In this case, entropy returns zero.
Notice that random devices may not always be available to produce random numbers (and in some systems, they may even never be available). This is signaled by throwing an exception derived from the standard exception on construction or when a number is requested with operator().
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.
// note: demo only: the performance of many
// implementations of random_device degrades sharply
// once the entropy pool is exhausted. For practical use
// random_device is generally only used to seed
// a PRNG such as mt19937
Just because it is newer does not mean it is better. The impression I got from a quick look at random_device is that it is best used to seed a pseudo random number generator as commented in that last link. That is the also the way I see it used in may examples at SO. In other words it is a replacement for srand(), not rand().