Assembly pseudo-random generator

I need a pseudo random number generator algorithm for the assembler program assigned in the course, and I would prefer a simple algorithm. However, I cannot use an external library.

What is a good, simple pseudo random number generator algorithm for assembly?

+5
source share
9 answers

It's easy to just pick two large relative primes a and b, and then continue to multiply your random number by a and add b. Use the modulo operator to keep low bits as your random number and keep the full value for the next iteration.

.

+8

2 . , , .

, . , , Mersenne Twister.

, , , , (, , API).

+3

, Crypto

, . 138

32- MOD 2^32

RNG = (69069*RNG + 69069) MOD 2^32
+3

, , C-Code, SSE. . , SSE.

#include <emmintrin.h>

static __m128i LFSR;

void InitRandom (int Seed)
{
  LFSR = _mm_cvtsi32_si128 (Seed);
}

int GetRandom (int NumBits)
{
  __m128i seed = LFSR;
  __m128i one  = _mm_cvtsi32_si128(1);
  __m128i mask; 
  int i;

  for (i=0; i<NumBits; i++)
  {

    // generate xor of adjecting bits
    __m128i temp = _mm_xor_si128(seed, _mm_srli_epi64(seed,1));

    // generate xor of feedback bits 5,6 and 62,61
    __m128i NewBit = _mm_xor_si128( _mm_srli_epi64(temp,5),
                                    _mm_srli_epi64(temp,61));

    // Mask out single bit: 
    NewBit = _mm_and_si128 (NewBit, one);

    // Shift & insert new result bit:
    seed = _mm_or_si128 (NewBit, _mm_add_epi64 (seed,seed));
  }

  // Write back seed...
  LFSR = seed;

  // generate mask of NumBit ones.
  mask = _mm_srli_epi64 (_mm_cmpeq_epi8(seed, seed), 64-NumBits);

  // return random number:
  return _mm_cvtsi128_si32 (_mm_and_si128(seed,mask));
}

. intrinsics SSE .

Btw - , 4.61169E + 18 . , 32- . .

+2

@jjrv
, , . . 0..N-1, N (32 32 , 64 ) 32 .

  a ( ), , Knuth ( 1, 3.3.4 TAOCP vol 2 1981), - 1812433253, 1566083941, 69069 1664525.

b. ().

+1

??? , ?

RNG , - .. rand() - - , ?

RNG, ? ? , ?

, . , RAM , , , :

  • 4- .
  • 32- CRC 4 . 4 .
  • 4 CRC32, . CRC32 8 .
  • , .

( crc32) , psuedorandom . , SSE. , CRC32 , .

+1

masm615 :

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 
+1

, , XOR sum , .

0

A linear congruent (X = AX + C mod M) PRNG may be suitable for an assembler course, as your students will have to deal with carry bits for intermediate AX results over 2 ^ 31 and module calculation. If you are a student, they are quite easy to use in assembly language and may be what the lecturer had in mind.

0
source

All Articles