C++ Random Number Question

Discussion in 'Webmastering & Programming' started by JC724, Apr 16, 2018.

  1. JC724

    JC724 [H]Lite

    Messages:
    73
    Joined:
    Jan 20, 2016
    This maybe a dumb question but I been googling and looking for an answer.

    How can I create a 16 BIT random number?
     
  2. Absalom

    Absalom Limp Gawd

    Messages:
    437
    Joined:
    Oct 3, 2007
    If you want to do it the C++ way, check out <random>.
     
  3. JC724

    JC724 [H]Lite

    Messages:
    73
    Joined:
    Jan 20, 2016
    All 16 bits are random. Like I can do .

    Can I do this ?
    int16_t bitNum = rand()%100; //bitNum in the range of 0 to 99.

    ?
     
  4. serpretetsky

    serpretetsky [H]ard|Gawd

    Messages:
    1,587
    Joined:
    Dec 24, 2008
    yes this will work:
    int16_t bitNum = (int16_t)rand()%0x10000; //bitNum in the range of 0 to 0xffff

    Note that rand() is not considered random enough for applications where a truly random number is required. Also, the use of %0x10000 causes the number to not be perfectly uniformly distributed. None the less, for most applications it will probably be good enough.

    Also, you may want to look into srand if you want to initiaze your random number generator with a random seed first (otherwise your code will produce the same sequence of random numbers every time it runs, which you may or may not care about.
     
    Armenius likes this.
  5. Aenra

    Aenra [H]Lite

    Messages:
    71
    Joined:
    Apr 15, 2017
    Would you mind explaining that further? Layman's terms a bonus. Not a software person, but not a stranger to math either; the above made me curious.
     
  6. jiminator

    jiminator Capt Obvious

    Messages:
    10,883
    Joined:
    Feb 2, 2007
    a 16 bit number can range from 0 to 65535. If you apply %100 to get a value from 0-100 then you will have 655 sets of numbers that go from 0 to 100 and 1 set that goes from 0 to 35. So the numbers 0 to 35 will appear with slightly more frequency. It is "fairly" random but not truly random. There are also problems in generating random numbers because the generation relies on techniques that are not random and can possibly be predicted. Many encryption exploits have been done based on this. Some programs when generating a random number will have you wiggle a mouse in a box and use that as input to their RNG.
     
    Aenra likes this.
  7. Absalom

    Absalom Limp Gawd

    Messages:
    437
    Joined:
    Oct 3, 2007
    rand() is a pseudo-random number generator. In math terms, it is deterministic. You can "predict" its outputs based on its inputs.

    Ideally, for something to be truly random, you want something leaning towards non-deterministic. What constitutes as a "good" random number generator is still of great debate today.

    Basically, digital computers are imperfect (or is that too perfect? hmm) for generating random numbers. Still, there are some really good RNG engines out there, such as those used by the C++ library I mentioned above. There are even some RNG engines that get assistance from the underlying hardware for introducing even more "randomness" into the result. Most of these hardware implementations rely on "noise" that is inherent of the analog signals found in the power delivery of the underlying circuits. This "noise" is often referred to as "entropy", which is just a fancy word for chaos. The more chaos in a system, the more potential for harvesting randomness.

    Unless you need NSA level encryption robustness, most of these standard library implementations are good enough for most projects.

    Coming up with one's own random number generator isn't that hard. You could literally use twitter as a source for a random seed. There's more random shit on the internet these days than any super AI could ever predict.
     
    Last edited: Apr 23, 2018 at 2:05 AM
    Aenra and Armenius like this.
  8. serpretetsky

    serpretetsky [H]ard|Gawd

    Messages:
    1,587
    Joined:
    Dec 24, 2008
    I'm assuming you are specifically asking about rand() and not why %100 makes it non uniform. I don't know too much about this subject, but here are some links that might give you more info:
    https://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful
    https://wiki.sei.cmu.edu/confluence...+function+for+generating+pseudorandom+numbers
     
    Aenra likes this.
  9. Aenra

    Aenra [H]Lite

    Messages:
    71
    Joined:
    Apr 15, 2017
    Would never have thought of that..
    Thanks to everyone for replying as well, i like learning new things :)

    Expected this to be complicated, but more like the other way round; like conceiving the way for a simple program to "pick" a trully "random" moment, but never thought of the pool itself.