![]() |
|
C++ True Random Generator - Printable Version +- Sinisterly (https://sinister.ly) +-- Forum: Coding (https://sinister.ly/Forum-Coding) +--- Forum: C, C++, & Obj-C (https://sinister.ly/Forum-C-C-Obj-C) +--- Thread: C++ True Random Generator (/Thread-C-True-Random-Generator) Pages:
1
2
|
C++ True Random Generator - Night Owl - 12-15-2013 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;
}RE: C++ True Random Generator - w00t - 12-15-2013 Nononononononononononononononono. Please. No. Seeding with the time at program start is really really bad. Like, really bad. Reaaallly bad. RE: C++ True Random Generator - Night Owl - 12-15-2013 (12-15-2013, 11:07 PM)w00t Wrote: Nononononononononononononononono. What do you suggest ? RE: C++ True Random Generator - The_Joker - 12-16-2013 (12-15-2013, 10:42 PM)Night Owl Wrote: Found on my old files tought it might be helpful to members 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. RE: C++ True Random Generator - Night Owl - 12-16-2013 (12-16-2013, 12:12 AM)The_Joker Wrote: This is far from a truly random number generator. At very best you could call this pseudo-random. Of course its not that complex you cant use it in gambling machines or anything like that it but its usefull for game devs or other simple tasks. RE: C++ True Random Generator - w00t - 12-16-2013 Code: int sTime = time();
for( int i=0; i < rand()%25; i++ ) do {}
srand( time()-sTime );Much more random, as the amount of time it would take every loop is random. RE: C++ True Random Generator - cxS - 12-17-2013 (12-16-2013, 12:23 AM)w00t Wrote: **is more random; These calculations are still all pseudo-random. Most of the time you don't really need something as random as you can get an algorithm to randomize things however. Even a good random generator still has possibility to generate suspicious non-random values. You can't get true random anyways. By all means, one *could* waste all the computing time they want to have something that works, but you've probably lost more than you've gained in doing so, depending on how much work you put into getting a more pseudorandom value. RE: C++ True Random Generator - w00t - 12-17-2013 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 ). RE: C++ True Random Generator - cxS - 12-17-2013 Theoretically, the more *random* you get, the more pseudorandom you get. Because to be truly random, the possibility of there being repetition in "randomness" is a likely possibility, just as much as there being diversity. And to play devils advocate, if you're going for a new number every time to hit those characteristics, the more predictable it becomes that you won't get the same number the second time around, thus, less random. This is one of the reasons why I don't really care for something that others may consider super random, because it's not really realistic; random. The irony. ![]() The only time it matters is if your specific case is special and you need those numbers to hit differently the majority of the time. RE: C++ True Random Generator - Anizeb - 12-17-2013 This reminds me of an article I read not all too long ago. "What does randomness look like?" Basically it's what cxS said, but explained in depth. |