Pseudorandom generator in Assembly Language

前端 未结 9 1022
予麋鹿
予麋鹿 2021-01-05 11:07

I need a pseudorandom number generator algorithm for a assembler program assigned in a course, and I would prefer a simple algorithm. However, I cannot use an external libra

相关标签:
9条回答
  • 2021-01-05 11:26

    Easy one is to just choose two big relative primes a and b, then keep multiplying your random number by a and adding b. Use the modulo operator to keep the low bits as your random number and keep the full value for the next iteration.

    This algorithm is known as the linear congruential generator.

    0 讨论(0)
  • 2021-01-05 11:38

    @jjrv
    What you're describing is actually a linear congrential generator. The most random bits are the highest bits. To get a number from 0..N-1 you multiply the full value by N (32 bits by 32 bits giving 64 bits) and use the high 32 bits.

    You shouldn't just use any number for a (the multiplier for progressing from one full value to the next), the numbers recommended in Knuth (Table 1 section 3.3.4 TAOCP vol 2 1981) are 1812433253, 1566083941, 69069 and 1664525.

    You can just pick any odd number for b. (the addition).

    0 讨论(0)
  • 2021-01-05 11:40

    Linear congruential (X = AX+C mod M) PRNG's might be a good one to assign for an assembler course as your students will have to deal with carry bits for intermediate AX results over 2^31 and computing a modulus. If you are the student they are fairly straightforward to implement in assembler and may be what the lecturer had in mind.

    0 讨论(0)
  • 2021-01-05 11:45

    Simple code for testing, don't use with Crypto

    From Testing Computer Software, page 138

    With is 32 bit maths, you don't need the operation MOD 2^32

    RNG = (69069*RNG + 69069) MOD 2^32
    
    0 讨论(0)
  • 2021-01-05 11:45

    Why not use an external library??? That wheel has been invented a few hundred times, so why do it again?

    If you need to implement an RNG yourself, do you need to produce numbers on demand -- i.e. are you implementing a rand() function -- or do you need to produce streams of random numbers -- e.g. for memory testing?

    Do you need an RNG that is crypto-strength? How long does it have to go before it repeats? Do you have to absolutely, positively guarantee uniform distribution of all bits?

    Here's simple hack I used several years ago. I was working in embedded and I needed to test RAM on power-up and I wanted really small, fast code and very little state, and I did this:

    • Start with an arbitrary 4-byte constant for your seed.
    • Compute the 32-bit CRC of those 4 bytes. That gives you the next 4 bytes
    • Feed back those 4 bytes into the CRC32 algorithm, as if they had been appended. The CRC32 of those 8 bytes is the next value.
    • Repeat as long as you want.

    This takes very little code (although you need a table for the crc32 function) and has very little state, but the psuedorandom output stream has a very long cycle time before it repeats. Also, it doesn't require SSE on the processor. And assuming you have the CRC32 function handy, it's trivial to implement.

    0 讨论(0)
  • 2021-01-05 11:46

    Using masm615 to compiler:

    delay_function macro
        mov cx,0ffffh
    .repeat
        push cx
        mov cx,0f00h
        .repeat
            dec  cx
            .until cx==0
        pop cx
        dec cx
        .until cx==0
    endm
    
    random_num macro
       mov  cx,64    ;assum we want to get 64 random numbers
       mov  si,0
    
    get_num:    
       push cx
       delay_function    ;since cpu clock is fast,so we use delay_function
       mov  ah,2ch  
       int  21h
       mov  ax,dx     ;get clock 1/100 sec
       div  num       ;assume we want to get a number from 0~num-1
       mov  arry[si],ah   ;save to array you set
       inc  si
       pop  cx
       loop get_num   ;here we finish the get_random number 
    
    0 讨论(0)
提交回复
热议问题