1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

A better way to generate random numbers

Discussion in 'Mathematics' started by amusicsite, 18 May 2016.

  1. amusicsite

    amusicsite dn ʎɐʍ sᴉɥ┴ Staff Member

    Location:
    UK
  2. Yellow Fang

    Yellow Fang Über Geek

    Location:
    Reading
    I recently used a random number generator to generate unique identifiers. The UIDs are 32 bit unsigned numbers. The random generator function returns a double precision number between 0 and 1. When I looked it up, double precision numbers have 53 bits for fractions, which is a lot more than 32. I left shifted the output of the random number generator by 32 bits and rounded to the nearest integer. It seems to work, but I wondered how truly random it was. The other way of implementing UIDs I read about was to just increment the number every time a record was generated, but I was concerned that would be more error prone. The number might get reset somehow. I don't know how, but maybe more than one device might generate them. 32 bits is 4 billion. With say 20 event records generated per day, the chance of getting two identical randomly generated records must be infinitesimal. However, if the numbers are not genuinely random, the chances may be much higher. I heard the boss say he was thinking of reducing the UID number to 16 bits, but I don't like the idea of that much.
     
  3. amusicsite

    amusicsite dn ʎɐʍ sᴉɥ┴ Staff Member

    Location:
    UK
    This is usually discouraged as people can then guess other UDIs. Typically a date in a good thing to use. Multiply a random number by the h/m/s or unix time stamp can make a less gamable random number.