How to sow to generate random numbers?

It does not generate a supposed randomness.

I thought starting from a seed like $ 66 and xoring are the last two least significant bits, and ror would give me the next random number and so on, but it only shows $ B3 and doesn't change at all.

How should I eat? The line above randomly displays the number on portc since I want the two numbers to be displayed one after another.

I am using avr studio 4 for atmega 8535 at 1 MHz.

> ;Program to random numbers on port C > > ;Stack and Stack Pointer Addresses .equ SPH =$3E > ;High Byte Stack Pointer Address .equ SPL =$3D > ;Low Byte Stack Pointer Address > > .equ RAMEND =$25F ;Stack Address > > ;Port Addresses > > .equ PORTC =$15 ;Port C Output Address > > .equ DDRC =$14 ;Port C Data Direction Register > Address > > .equ numberoneddr=DDRC > > .equ numberoneport=portc > > .equ numbertwoddr=DDRC > > .equ numbertwoport=portc > > .equ delayCount=21 > > .equ random1 =$66 > > ;Register Definitions > > .def numberone =r1 ;Register to store data pointed > to by Z > > .def numbertwo =r2 > > .def temp =r16 ;Temporary storage register > > > > reset: > > ; initialize stack pointer. Done automatically at reset on many AVRs > > ldi temp, low (RAMEND) > > out spl, temp > > ldi temp, high (RAMEND) > > out sph, temp > > > > > ;port initialisation > > ldi temp,$FF > > out numberoneddr,temp > > out numberoneport,temp > > out numbertwoddr,temp > > out numbertwoport,temp > > > > ;Program Initialisation > > ldi temp,$66 > > rcall random > > mov numberone, temp > > out numberoneport,numberone > > rcall random > > mov numbertwo, temp > > out numberoneport,numbertwo > > > > > random: mov r19,temp > > ldi r17, 0x01 > > eor r19,r17 > > ror r19 > > mov temp,r19 > > ret > > > > delay: > clr r20 > > clr r21 > > ldi r22, delayCount > > loopDelay: > > dec r20 > > brne loopDelay > > dec r21 > > brne loopDelay > > dec r22 > > brne loopDelay > > ret 
-one
assembly avr
source share
1 answer

After some searching in the archives of the ancient asm source code, I found this for the x86 MSDOS NASM platform that I used in the days:

 ;.rnd ;al=rnd num <0,ah>; .rnd: pusha mov cx,ax .rnd0: mov bx,[cs:.rnddat] mov ax,[cs:.rndtim] xor al,bh add ah,bh rcr ax,3 xor al,bl rcl ax,2 .rnd2: cmp al,ch jbe .rnde sub al,ch or ch,ch jnz .rnd2 sub al,al .rnde: mov ah,bl mov [cs:.rnddat],ax or al,1 xor ax,[fs:046Ch] add [cs:.rndtim],ax popa mov al,[cs:.rnddat] ret .rnddat:db 0,0 .rndtim:dw 0 

The idea is that some stored numbers perform some basic ALU operations, such as +,*,/,<<,>>,&,^ , but ensure that there is no saturation and is usually replaced with H,L some value, to keep randomness in check. Therefore, transfer this to your asm , but I highly recommend that you encode it and try it on your PC first to make sure that randomness is in order for your task.

BTW you can also use program memory or any ROM content as a base for randomness ... it also uses an internal RTC block, so you need to omit this part or add a timer or just instead be through a chain of non-empty data.

 [0000:046C] are 4 Bytes master clock count (long integer) 0 = midnight and increments until a 24 hour equiv. 

I found an even older demo called NoSignal (since 1997 at TASM), which is inside:

  .386P IDEAL MODEL TINY CODESEG STARTUPCODE main: mov ax,19 ;320*200*256 int 16 push 0A000h ;Video segment pop es ;keyboard test,speaker delay v si=256 l0: ror ax,cl ;rnd...ax add ax,di stosw ;plot... loop r1 ;speaker delay... mov cx,si out 61h,al r1: or di,di jnz l0 push ax mov ah,1 ;test keyboard int 16h pop ax jz l0 ende: sub ax,ax ;turn off speaker and exit out 61h,al int 16h mov ax,3 int 16 ret END 

It fills the screen and speaker with white noise, as if there was no antenna cable in the analog TV. This version is 44 bytes long, the pseudo-random generator starts with the label l0:

  • ax is the generated number (as well as the previous generated number, e.g. temp)
  • di increases (something like actual time) ...
  • cl decreases

therefore, if I look at it correctly, this should be enough:

  rnd:ror ax,cl ;rnd...ax add ax,di inc di dec cl ret 

and add push/pop if necessary save registers / values. If you need something more complex, use modulo prime arithmetics.

[edit1] simple C ++ pseudo-random generator

 WORD rnd_d0=0x66; // these are seed numbers if not selected right then the randomness is not good WORD rnd_d1=0x5A; // these give fairly good results WORD rnd_d2=0xC3; WORD rnd() { rnd_d0^=rnd_d1|rnd_d2; // xor rnd_d1*=rnd_d2; // mul rnd_d2+=rnd_d1; // add rnd_d0=(rnd_d0<<8)|(rnd_d0>>8); // 8bit halves swap return rnd_d0; } 

The above random generators have been compressed to DOS environment time or special use. This is not ... an accident is this:

randomness graph

when I use it to populate the NoSignal image window, the result is as follows:

Nosignal

and here Gifa :

NoSignal 320x240x3

NoSignal fill code is as follows:

  for (int y=0;y<ys;y++) for (int x=0;x<xs;x++) pyx[y][x]=0x00010101*int(rnd()>>8); 

Thus, only a high value of 8bit from a pseudo-random number of 16bit , multiplication simply converts this 8bit number to gray scale color.

  • xs,ys - image size
  • pyx is a direct pointer to its lines.

Do not change seed numbers without proper testing with this on PC

Incorrectly selected seeds do not lead to randomness at all. If you want to sow safely (without testing), then run with the constants provided, and then call rnd() as many times as your new seed number. Busted it right now, so there might be better seeds for this, these are just the first ones I found, which gives good results

These seeds are also good:

  WORD rnd_d0=0x37A6; WORD rnd_d1=0x377A; WORD rnd_d2=0x3BC3; 
+1
source share

All Articles