Search code examples
assemblyx86masm

Generating random 32bit number in assembly


I'm looking for an assembly function that generate random number from 1 to n. n value could be about 60000. I have no idea how to do that. I've searched for this and i couldn't find any that met my excpectations.


Solution

  • Linear Congruent Generator:

    r[n+1] = (a * r[n] + c) % m
    

    m = 65537 (216+1)

    a = 65538 (a - 1 must be multiple of any prime divisor of m, and single divisor is m itself, i.e. 65537)

    c = any even number (c and m must be relatively prime)

    [Knuth, II vol., 3.2.1.1--3.2.1.2]