How do you generate random numbers in assembly code?

Discussion in 'Computer Science & Culture' started by Voodoo Child, Mar 6, 2003.

Thread Status:
Not open for further replies.
  1. Voodoo Child Registered Senior Member

    Messages:
    1,296
    for the x86
     
  2. Google AdSense Guest Advertisement



    to hide all adverts.
  3. Reid Registered Senior Member

    Messages:
    97
    I'm not sure but I think theres a instruction called
    rand
    or
    rnd
    look them up
     
  4. Google AdSense Guest Advertisement



    to hide all adverts.
  5. Voodoo Child Registered Senior Member

    Messages:
    1,296
  6. Google AdSense Guest Advertisement



    to hide all adverts.
  7. NanoTec Registered Senior Member

    Messages:
    30
    It varies by compiler however,
    Library calls from assembly ease the process,

    Extern rand
    Extern srand

    push eax; Push a seed value in eax to the stack.
    call srand; Seeds the random number generator with the top of the stack.
    add esp,4; Cleans up the stack

    push ecx; save eax - rand destroys it
    call rand; places random number in ecx
    pop ecx; restore eax

    It only returns a 31-bit unsigned value in x86 linux, and specifically this method has problems generating randomness in the lower order bits due to some statistical skew. The tables are only so big, some numbers never come up.
    Usually the seed value is only set once and done by using the seconds count since 1970 or some other random value, however no matter what it is still a giant table of random numbers being called so the results could be predicted if one knew the seed value or repeatedly used the same seed value.
    Generating truly random numbers is a real headache especially for computers designed to operate the same way every time.
     
  8. Blindman Valued Senior Member

    Messages:
    1,425
    Try using a timer register; this has worked well for me especially if the random call is made on irregular intervals. It doesn’t work to well if you are calling it on regular intervals, such as after waiting for screen sync.

    I have for a long time just used a 1024 byte array containing each value between 0 - 255 4 times each, randomly placed in the array.

    Again it is not the best solution but works very well in giving the appearance of randomness.
     
Thread Status:
Not open for further replies.

Share This Page