Is this the right way to generate random number in ASM prog

Started by adityakiran, August 11, 2005, 03:36:19 PM

Previous topic - Next topic

adityakiran

Hi all, is there any seperate function to generate random numbers in ASM. I am using a NASm compiler.
I actually wrote this by calling a .c file that generated random numbers using the C's rand function and doing small maipulations like

   mov ebx,ecx
   call _randnum
   mov [esi],eax
   mov ecx,ebx
   add esi,4
   loop fun


   mov ecx,100
   mov esi , input1

Is there any better way to do it? please suggest me..!!

Thanks, Adityakiran

adityakiran

i am a beginner trying to learn asm stuff through onlien tutorials, so plez help..!!

Thanks, Adityakiran

hutch--

adityakiran,

You have a couple of choices, you can use the nrandom procedure in the masm32 library or you can use the MSVCRT function from the C runtime DLL, both of which are easy enough to do in masm32.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

Mark Jones

Hutch, haven't we tested nrandom previously and found its chi2 unacceptable? Here's the thread:

http://www.masmforum.com/simple/index.php?topic=555.0
"To deny our impulses... foolish; to revel in them, chaos." MCJ 2003.08

hutch--

Mark,

The last version I had was fixed by a number of people who did the testing on it with John Walker's ENT program. The later mod improved its chi squared preformance by a lot.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

roticv

I am quite puzzled. What distribution are you testing against when applying chi square test?

hutch--

Victor,

This one from the last service pack, it was modified late last year.


; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
;
;                     Park Miller random number algorithm.
;
;                      Written by Jaymeson Trudgen (NaN)
;                   Optimized by Rickey Bowers Jr. (bitRAKE)
;
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

      .486                      ; create 32 bit code
      .model flat, stdcall      ; 32 bit memory model
      option casemap :none      ; case sensitive

    .code

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

align 4

nrandom PROC base:DWORD

    mov eax, nrandom_seed

  ; ****************************************
    test eax, 80000000h
    jz  @F
    add eax, 7fffffffh
  @@:   
  ; ****************************************

    xor edx, edx
    mov ecx, 127773
    div ecx
    mov ecx, eax
    mov eax, 16807
    mul edx
    mov edx, ecx
    mov ecx, eax
    mov eax, 2836
    mul edx
    sub ecx, eax
    xor edx, edx
    mov eax, ecx
    mov nrandom_seed, ecx
    div base

    mov eax, edx
    ret

nrandom ENDP

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

nseed proc TheSeed:DWORD

    .data
      nrandom_seed dd 12345678
    .code

    mov eax, TheSeed
    mov nrandom_seed, eax

    ret

nseed endp

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

    end
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

JHER VON ARBANEL

 :thumbu well i have a better algorithm look
   :bdg first i ask the  clock of my pc and i work wtih the seconds,i just do an shl eax,1 and i have i random number or  :bdg if u want when u have the tiem time y can add hours minutes and seconds and u would have a better one ,, i hope it would be helpful :bdg

ToutEnMasm

Hello,
nrandom is not a random number generator it's only a generator of suit of numberS.
The first number will be always the same and the second always the same .If you change the input number you got another suit.If you try to change the input by the number generated , the suit tend to zero.
It's not random.
                                  ToutEnMasm

Mark Jones

It baffles me that the Intel/AMD guys haven't made a hardware RNG. It's surprisingly simple to do! Just reverse-bias a P-N semiconductor junction into the avalanche region, then buffer the resulting "noise" that is generated. It doesn't get any simpler or random than electron avalanche. You'd think with the millions and millions of existing transistors on the average processor today, they'd sacrifice ONE for this purpose? ::) :bg
"To deny our impulses... foolish; to revel in them, chaos." MCJ 2003.08

roticv

hutch, that does not answer my qn. Are you saying that the chi square testing is compared against the algorithm by NaN?

OMG. OKAY these days i wouldn't trust computer programmers.

hutch--

Victor,

Have I missed something here, a couple of the members battered nrandom to death using John Walkers ENT random data testing program. It had a poor chi-square test so it was modified. The thread with the tested results were in the Laboratory about 6 months ago.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

roticv

I have recently been enlightened in the field of statistics. So here's some of my comments.

In the computation of the chi squared value, there is a need to have the expected value. From the way I look at it, the chi squared value calculated by John Walker's ENT random program should be trusted because he did not state clearly what is he testing against.

Yes chi squared is used as a test against distributions, checking whether the data follows any distribution.

zooba

I would imagine it was tested against the uniform distribution, as this is the intention of a good RNG  :toothy

Mark Jones

Quote from: ToutEnMasm on August 14, 2005, 04:56:03 PM
nrandom is not a random number generator it's only a generator of suit of numbers.

From my humble experience I have to agree. Once I made a proc which fed the system up-time counter (in milliseconds) to nrandom with a static seed value. No matter what system time I fed it, it always returned the same values - in order. If I changed the seed, I'd get different output numbers, but there is an obvious 100% correlation. I no longer have the code but it could have been buggy, or just a flawed implementation of the nrandom proc. I gave up on that project because I couldn't get a random number sequence with any margin of entropy or chi2 distribution.
"To deny our impulses... foolish; to revel in them, chaos." MCJ 2003.08