How do you guys limit rand() results to a range?

Discussion in 'C Programming' started by DFS, Jun 1, 2014.

  1. Yes. I had thought for that reason that they were using very low
    bit rates, such as 1bit/s (slow enough that you can generate one
    photon at a time, and be sure it is gone before the next one).

    According to:

    they now have some up to 1Mbit/s.
    As noted before, one way to remove bias from a hardware random
    bit generator is to XOR with a cryptographically secure PRNG.

    I suppose one could also use two different secure PRNGs and
    XOR them together.

    -- glen
    glen herrmannsfeldt, Jun 6, 2014
    1. Advertisements

  2. DFS

    Martin Shobe Guest

    To remove bias? That probably won't work. If there's some correlation
    between the PRNGs, then you'll be adding bias. The fact that they are
    different secure PRNGs isn't sufficient to prevent that.

    Martin Shobe
    Martin Shobe, Jun 7, 2014
    1. Advertisements

  3. DFS

    Stefan Ram Guest

    The measurement operators indeed did some transformation of
    the quantum data to reduce a bias (Neumann-type normalization):

    »the even-parity sequences "00" and "11" are discarded,
    and only the odd parity ones "01" and "10" are kept.«
    ... and mapped to 0 and 1, respectively.

    It might be possible that some »unbias effort« might have
    distorted some statistical properties somehow. Maybe it would
    help to apply all »unbias« operations always to /all/ sources
    of randomness measured (i.e., also to the classical ones).
    Stefan Ram, Jun 7, 2014
  4. "Ben Bacarisse" wrote in message
    Thank you Ben for taking note of the massive hack folly
    ingrained within the posted code. I had a massive brain
    melt here. I do not need to wait for a non-zero number
    from `rand()' added to the digit sum of the previous.

    Why I posted that hack shi%. Well, I have no good answer!


    Chris M. Thomasson, Jun 9, 2014
  5. I would say that is a rather minor point! I take it you've sorted it
    out now, and either don't need this or have a better method?
    Ben Bacarisse, Jun 9, 2014
  6. "Ben Bacarisse" wrote in message
    This was way back when I was learning how to program BASIC.

    Are you speaking about the digits being way too similar? ;^)

    Basically, you can bet on two, or three numbers being more
    probable. Well, I do remember spreading out the high
    probability's across the lowest probabilities. Sort of hiding
    them within.

    BTW, IIRC, I was using this random digit 0..9 because I
    had 10 games, and I was renting to my friends. This was
    back in BASIC on my IIGS.
    Chris M. Thomasson, Jun 9, 2014
  7. Yes, there's a strong bias in the digits. That and the odd way you
    print the digits in a number rather than printing the number! The whole
    thing looked very odd to me.
    Ah, I would not dare to say what sort of programs I wrote in those
    Ben Bacarisse, Jun 9, 2014
  8. DFS

    Tim Rentsch Guest

    Why not? I see no reason it shouldn't.
    Tim Rentsch, Jun 10, 2014
  9. DFS

    Tim Rentsch Guest

    Yes, but far less than the probability that some register
    bits will quantum mechanically flip undetectably even if
    they have ECC. So it isn't the sort of thing worth losing
    any sleep over.
    Tim Rentsch, Jun 10, 2014
  10. (snip)
    Or alpha particles, which are much more common.

    -- glen
    glen herrmannsfeldt, Jun 10, 2014
  11. DFS

    Noob Guest

    Noob, Jun 10, 2014
  12. DFS

    Tim Rentsch Guest

    I expect you mean something like this -

    uniform_below( unsigned n ){
    static uintmax_t w = 1, x = 0;
    // check to be sure 1 <= n <= RAND_MAX
    unsigned minimum = (RAND_MAX - (n-1)) % n, t;

    while( 1 ){
    if( n <= w && x >= w%n ){
    t = (x-w%n) % n;
    w = 1, x = 0;
    return t;

    t = rand();
    if( t >= minimum ) return (t-minimum) % n;

    // put in some safeguard against w overflow
    w *= minimum, x = x*minimum + t;

    Note that this method doesn't always help. For example, if
    RAND_MAX is 32767, and what's wanted is a random number less
    than but not equal to 32767, then there is only one unusable value,
    and getting that value adds no information to the pool of
    unused information. In practice though an approach along these
    lines will always return quickly, and will make less use of
    the underlying RNG than just discarding unusable values.
    Tim Rentsch, Jun 10, 2014
  13. DFS

    Phil Carmody Guest

    Thank you Tim! I don't recognise that exact formulation,
    as it had n as a parameter, so was more general than
    the situation I last coded (where you created the object
    at the start, and then just polled it for new numbers
    in an unchanging range, classic base-conversion). but
    the general form was spookily similar.

    I must see if I can find a machine with a floppy drive,
    and before that see if I have any of my old backup
    floppies to see what I once used way back in the glory

    Phil Carmody, Jun 10, 2014
  14. DFS

    Tim Rentsch Guest

    Thank you, I had fun doing it.
    Tim Rentsch, Jun 12, 2014
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.