Thirteen Years of Service
Posts: 1,256
Threads: 7
RE: C++ True Random Generator 12-15-2013, 11:07 PM
#2
Nononononononononononononononono.
Please.
No.
Seeding with the time at program start is really really bad.
Like, really bad.
Reaaallly bad.
•
Thirteen Years of Service
Posts: 112
Threads: 17
RE: C++ True Random Generator 12-16-2013, 12:12 AM
#4
(12-15-2013, 10:42 PM)Night Owl Wrote: Found on my old files tought it might be helpful to members
Code:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
srand(time(0));
for (int x = 1; x<10; x++)
{
cout << 1+(rand()%6) <<endl;
}
return 0;
}
This is far from a truly random number generator. At very best you could call this pseudo-random.
Computers don't understand the concept of random. You need to give them something to start with, then manipulate. In this case, the seed is the system time, which is a poor system, are there are a very finite number of system times available. If you wanted to increase the randomness, you would need to include other seeds such as keystroke patterns and mouse position. Many cryptographic algorithms have been broken due to a weak random number generator.
Pay respects to the malformed SYN packet.
•
Thirteen Years of Service
Posts: 1,256
Threads: 7
RE: C++ True Random Generator 12-17-2013, 05:39 AM
#8
Well, yes, deterministic machines can't create real randomness. A good PRNG passes 3 tests I can't recall in detail right now, but basically thousands of numbers are generated and the PRNG fails if any number, sequence of numbers, or multiples of a number are more common than any other number( usually with a percent or so leeway ).
•