|
|
#1 |
|
Good, Portable Random Number Generator for C++?
Is there a good, portable random number generator available for C++?
By "good", I mean I don't have to include a line of code to seed it, it'll be different with each run of the program, and the pattern won't be apparent. I know that rand() is available, but issues with it include: 1 - You have to seed it. 2 - It's implementation dependent, and there's no requirement for it to actually have a non-predictable pattern. An implementation could be as simple as: Code:
int rand() {
return 0;
}
If I were writing this program for just OS X and iOS, I'd use arc4rand()... does anyone have anything comparable I could use in C++?
__________________
Battery Status - On the Mac App Store
The only app that'll estimate when your wireless devices will need their batteries changed. Like it on Facebook! |
|
|
|
0
|
|
|
#2 |
|
Google the source for arc4rand.
I did it myself a while ago, because I needed a PRNG in an embedded microcontroller project. It wasn't hard to find C implementations. You will still need a source for initial entropy, and that source may need to be tailored to the platform. For example, one of the default sources in the source I got was the uninitialized stack area. On an embedded uC, that's an extremely predictable source, because the call sequence that leads up to the initialization is always identical. So for me, the hard part wasn't the PRNG itself, but coming up with a usable source of initial entropy. A typical default source on a non-embedded computer is the /dev/random device (or its equivalent). If you know that device will be present, use it. Otherwise the task of gathering entropy becomes harder. |
|
|
|
0
|
|
|
#3 |
|
Actually, there's a requirement that it have a predictable pattern.
What is sufficiently non-apparent for a particular intended application depends on your particular needs. These may be suitable for some purposes: http://www.math.sci.hiroshima-u.ac.j...at/MT/emt.html http://csrc.nist.gov/groups/ST/toolk...om_number.html |
|
|
|
0
|
|
|
#4 |
|
Code:
cout << "Please roll a die and type in the result: " ; cin >> result ; ![]() Seriously, though, any pseudorandom number generator is going to have to be seeded somehow, and will by definition have a predictable pattern. The only way around this is with a dedicated hardware random number generator (of which, technically, a thrown die is an example). As chown33 said, if the problem is that you don't have any way of generating the initial entropy required to seed the generator on your system, then you're simply not going to find any library that will work for you. If you do have access to something like a continuously running timer, then what's the problem with seeding? If you really want, you can write a wrapper around random() (which is a better replacement for rand() and already part of stdlib.h) that reseeds the PRNG each time you call it. That would make it less "predictable", anyway, assuming you didn't call it so quickly that you ever generate numbers with the same random seed twice in succession. |
|
|
|
2
|
|
|
#5 | |
|
Quote:
#include <random> Your other requirements are either easy to deal with or impossible to achieve using ordinary equipment. |
||
|
|
0
|
|
|
#6 | |
|
Quote:
__________________
2012 11" MBA i7/8/256 2011 Mac Mini with 27" Thunderbolt Display Black iPad Mini 32GB Verizon Black iPhone 5 32GB ATT
|
||
|
|
0
|
|
|
#7 |
|
Open terminal, enter "man arc4random" and read carefully.
You can't produce better random numbers. Well, if you can, you are not looking for advice on macrumors
|
|
|
|
0
|
|
|
#8 | |
|
Quote:
http://en.wikipedia.org/wiki/RdRand
__________________
Neural Advance - Mac OS X, UNIX and Windows Development Last.fm Profile | Extreme Metal Reviews MP 4x 2.66Ghz Xeons / 6GB RAM / 640GB + 500GB + 750GB + 1TB HDDs / ATI Radeon 4870 / iPad 3 |
||
|
|
0
|
|
|
#9 | |
|
Quote:
|
||
|
|
0
|
|
|
#10 | |
|
Quote:
I'd like my app to run on darn near any OS... OS X, iOS, Android, Windows XP, 7, RT, or 8... and it must be C++. So being able to seed with a clock isn't an actual issue, but I prefer systems where I don't need to handle seeding (IE, the way arc4rand() doesn't need to be seeded first.)
__________________
Battery Status - On the Mac App Store
The only app that'll estimate when your wireless devices will need their batteries changed. Like it on Facebook! |
||
|
|
0
|
|
|
#11 |
What's the point of bloating your program unnecessarily with some third-party random number library instead of just using the perfectly good built-in random number function, all to avoid a single line of code?
|
|
|
|
0
|
|
|
#12 | |
|
Quote:
- Current configuration data in preference files (user specific). - Current uptime (microseconds or nanoseconds). - Number of files in a certain folder (user specific). - All names of all files in a certain folder (user specific). - Noise from the microphone & camera pictures (user specific). - Random seed files from other (open source) applications (openssl for example). For preshared keys (SHA-2 (512-Bit) or SHA-3 based), i would use noise from a non-silent street/city recorded via a stereo recorder like the WS-803 or WS-813. This device supports the lossless PCM format in WAV files (41 kHz). If someone can predict 1 GB of this noise, then i'm the emperor of china! ;-)
__________________
OS X 10.9 and iOS 7 delayed. Haswell Q3/Q4 2013. -------------------- “Only the dead have seen the end of the war.” -- Plato --
|
||
|
|
0
|
|
|
#13 |
|
Which "perfectly good built-in random number function" do you mean? The one in the Standard C library?
|
|
|
|
0
|
|
|
#14 | |
|
Quote:
I have, for the time being, decided to just use it and, to ensure "fairness" to the best of my ability, I include something like: Code:
unsigned char n = (from user input);
int maxFairRand = (MAX_RAND / n) * n;
int rand = random();
while (rand > maxFairRand)
rand = random();
unsigned char r = rand % n
I'm just doing a school homework assignment that involved an RNG as a portion of it. I figure overdoing an assignment is always fun, so I tried that here. |
||
|
|
0
|
|
|
#15 |
|
seed sources
Any thoughts on these for "random seeds"
Internal: system clock milliseconds External: Earthquake web site richter scale fractions. |
|
|
|
0
|
|
|
#16 | ||
|
Quote:
---------- Quote:
Better: /dev/random. |
|||
|
|
0
|
|
|
#17 | |
|
Quote:
).
|
||
|
|
0
|
|
|
#18 | ||
|
Quote:
http://c-faq.com/lib/randrange.html As for the code you presented, I don't recognize random() and MAX_RAND. For Standard C, I know about rand() and RAND_MAX, which are in <stdlib.h>. Since you asked about C++ in your original question, I gave you a C++ answer in my earlier reply: Quote:
ETA: As you say you're doing homework and like to overdo things, do you understand why the C FAQ code I linked to uses unsigned int and the constant 1u in the statement, "unsigned int x = (RAND_MAX + 1u) / N;" Hint: It could have achieved the same thing with ((unsigned int) RAND_MAX+1)/N. One or the other is necessary! Last edited by jon3543; Jan 10, 2013 at 02:05 PM. |
|||
|
|
0
|
|
|
#19 | ||
|
Quote:
Quote:
Suppose RAND_MAX is 4 and I want one of 3 values. My code, would do 4 / 3 = 1, then * 3 = 3. Which means it could return 0, 1, 2, or 3... I see the issue here... I should discard values >= maxFairRand rather than only greater than it. Or is that not the issue you're getting at?
__________________
Battery Status - On the Mac App Store
The only app that'll estimate when your wireless devices will need their batteries changed. Like it on Facebook! |
|||
|
|
0
|
|
|
#20 | ||
|
Quote:
http://msdn.microsoft.com/en-us/library/bb982398.aspx The dropdown "Other Versions" says it was in VS2008. Quote:
The question I posed in my "ETA" concerns some lower-level behavior, which is still very important to understand. If you get it, great; if not, post something. |
|||
|
|
0
|
![]() |
|
«
Previous Thread
|
Next Thread
»
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
All times are GMT -5. The time now is 05:39 PM.








2012 11" MBA i7/8/256
What's the point of bloating your program unnecessarily with some third-party random number library instead of just using the perfectly good built-in random number function, all to avoid a single line of code?
Linear Mode
