More FAQ updates

Pages: 1234
(If you can come up with a long identifier like that that is exactly 31 characters long, let me know.)

A camelCase example?

const bool thisIdentifierHasExactly31chars = true;

Andy

PS In "real life" I've only ever come across such long identifiers in security related code. For example, remoteHostSecurityDescriptorKey, StringSecurityDescriptorLen, serverAuthenticationCredentials. Once you have 25 characters with AuthenticationCredentials it's easy to break through the 31 character barrier!
Last edited on
Ah, sorry, I already created that one. :-@
I should have specified underscore_notation.

Anyway, back to work with rand().
I always seem to get to these after people need them in the forum.

Unless, of course, you're a certain someone who is smarter than everyone else, able to defy mathematics and use a LCG to produce true random numbers... LOL
Alright, I think I've finally got the rand() FAQ done.
http://www.cplusplus.com/faq/beginners/random-numbers/

I still have to write the 'how to actually get a random number the Right Way' FAQ, which I'll probably work on next...

Now, I've spent a lot of time studying random numbers and the math behind them, but that doesn't mean I got it right. Please let me know if I messed anything up!

Also, major cookies to anyone who can fix my [citation needed].

[edit] Oh, also, I refrained from disparaging the arc4random* and drand48* functions. But I'm still thinking I ought to.
Last edited on
Otherwise, if you only plan to pull a relatively small number of random values, just keep track of which ones you have pulled in an array. If you pull a value that is already in the array, simply discard it and pull another.
I... do not like this approach. It might be acceptable if you are not allowed or cannot change set of possibilities (like there are too many of them to be actually stored in memory), but for everything else I prefer "take a pick, move (swap) last array element in place of picked, decrease pool size by 1".

The best way to do that is to simply use a local RNG in each thread
thread_local variables work wery well too and sometimes easier to set up.

Also, major cookies to anyone who can fix my [citation needed].
This depends on an algorithm, it seems. Mersenne Twister FAQ mentions that you can contakenate bits of 2 32 bit integers to get 64 bit one. http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/efaq.html
And I often hear that generators in C++ should be called RandomBitGenerator, where distributions are used to make Numbers.

After further research through it looks that it is safe only if two outputs are uncorrelated: http://crypto.stackexchange.com/questions/18591/entropy-of-two-concatenated-random-values
For example, in LCG last bit either always the same or flips on each operation (depending on constants), so if we glue together two values, they will have bits which are easely predictable.
Re: small set approach
It depends on where you wish to offload processing time. It would be a lot faster, in total, to simply produce a unique set of values and then peel them off when requested, but the overhead of doing that is then shifted to some place at the beginning of your program, where there might be, in combination with other initializations, a noticeable delay. There is also an issue on how much space you wish to allocate initially vs required.

In any case, the swap operation is heavier than a simple index lookup into the array.
- done: updated this answer also

Re: thread_local
Yes, worth using. Useless with rand(), though...
I'll add it to the C++ blurb.
- done.

Re: entropy
Yes! That is fabulous!
- done
You get a major cookie from me. (What would you like?)

Stupid browser dictionary doesn't have "initialization"; it underlined it in red. I'm an Object Pascal guy; I've typed that word countless thousands of times. Made me have to go look it up online to make sure my brain didn't break and misspell it. Stupid browser dictionary...
Last edited on
Topic archived. No new replies allowed.
Pages: 1234