flat assembler
Message board for the users of flat assembler.
Index
> DOS > Random numbers Goto page 1, 2 Next 
Author 

OzzY
How to generate a random number between 0 and 9??
Thanks!! 

01 May 2004, 19:21 

OzzY
Nobody knows?


01 May 2004, 21:51 

comrade
take current time, divide by 10, take remainder


01 May 2004, 22:00 

OzzY
Could you give me an example? =P
I'm having problems when trying to divide th number, I allways get an error... Thanks 

01 May 2004, 22:25 

comrade
Code: push ds push 040h pop ds mov ax, [ds:6Ch] xor dx,dx mov cx,10 div cx pop ds ; dx = number [0,9] 

01 May 2004, 22:44 

OzzY
I don't know how to get time without using int's... If I don't want to use int's in my program, how do I know the address of the int I want to use?
Thanks 

02 May 2004, 17:58 

Vortex
OzzY,
To generate random numbers, you use the rand function provided by C runtime DLLs. Check the attachment for an example.
_________________ Code it... That's all... 

02 May 2004, 19:09 

OzzY
Oh yeah, but i'm programming for DOS....
Do you know how? Anyway, do you know where can I find some good tutorials for win programming for FASM?? Thanks!! 

02 May 2004, 19:17 

neonz
OzzY wrote: I don't know how to get time without using int's... If I don't want to use int's in my program, how do I know the address of the int I want to use? You can get time from RealTime Clock (RTC) chip directly. http://www.nondot.org/sabre/os/files/MiscHW/RealtimeClockFAQ.txt 

02 May 2004, 20:57 

OzzY
How can I do this:
Code: push ds push 040h pop ds mov ax, [ds:6Ch] xor dx,dx mov cx,10 div cx pop ds ; dx = number [0,9] using INT's ? 

03 May 2004, 00:18 

Intrinsic
I don't know off the top of my head, but if it can be done(i would assume so) then i would hope you already have a copy of Ralf Brown's Interrupt list ( http://www2.cs.cmu.edu/afs/cs/user/ralf/pub/WWW/files.html ) and that'll contain all the info you need.


04 May 2004, 10:13 

Vion
Ok, the following i found on
http://www.npaci.edu/online/v3.7/SCAN1.html Hope this helps you, htere are more listed: "Linear Congruential Generator (LCG) The most commonly used random number generator is the LCG. It is based on the following iterative scheme: X n = a X n1 + c (mod m), where m is the modulus, a the multiplier, and c the additive constant. The size of the modulus constrains the period, and is usually a prime or a power of 2. When a, c, and m are chosen appropriately, one can obtain a random number sequence with maximum period equal to m. Following are some of the properties of LCGs : for m = 2M and c > 0, full period m (= 2M) is obtained if and only if a = 1(mod m) and c is odd for m = 2M and c = 0, maximum period is 2(M2) and it is obtained if and only if a = 3 (mod 8) or a = 5(mod 8) and initial seed is odd for m = p (a prime number) and c = 0 or c = nonzero, the maximum period is (p1) and is obtained if and only if a is primitive element modulo p " 

04 May 2004, 15:07 

comrade
Mersenne Twister is nice too
http://wwwpersonal.engin.umich.edu/~wagnerr/MersenneTwister.html http://home.ecn.ab.ca/~jsavard/crypto/co4814.htm 

04 May 2004, 16:56 

Jaques
http://www.agner.org/random/
has some good random # generators 

19 Jun 2004, 03:13 

prino
Very short, very fast and very good:
http://www.jstatsoft.org/v08/i14/xorshift.pdf 

24 Jun 2004, 17:36 

Bitdog
Nasm proc, might need converting,
returns 0255 Code: ; ========================= RANDOM.INC ========================= ; Procedure, RANDOM, generates random numbers in AL reg (not many 38's though) ; It gets every number 0255 with no appearant pattern in even amounts ; ; Call with, NOTHING ; ; Returns, AL = random number between 0255, ; AX may be a random number too ? ; DW RNDNUM holds AX=random_number_in AL ; SEED DW 3749h RNDNUM DW 0 align 16 RANDOM: PUSH DX MOV AX,[SEED] ;; AX = seed MOV DX,8405h ;; DX = 8405h MUL DX ;; MUL (8405h * SEED) into dword DX:AX ; CMP AX,[SEED] JNZ GOTSEED ;; if new SEED = old SEED, alter SEED MOV AH,DL INC AX ; ROR AX,1 ;; old version missed #38 a lot GOTSEED: MOV WORD [SEED],AX ;; We have a new seed, so store it MOV AX,DX ;; AL = random number MOV WORD [RNDNUM],AX POP DX RET 

05 Jul 2004, 01:25 

Matrix
Random numbers are more random on windows :DDD
MATRIX 

08 Sep 2004, 12:19 

Matrix
Well, maeby the simplest is the best, this was originally what i wrote first, but after a little modification it seems like its better than the ones i have developed from the first version ( look at the upper line and you'll see )
sorry if it won't exit, in some cases when the mainloop takes very little amount of time, i shouldn't use bios interrupt. Code: org 256 call set320x200x256 call initrandomz mainloop: mov ecx,320 call randomz mov bx,ax mov ecx,200 call randomz mov cx,ax push ecx mov ecx,255 call randomz pop ecx call putpixel320x200x256 ; al=color bx=x cx=y push eax call bkeycheck cmp al,27 pop eax jnz mainloop call set80x25t int 20h initrandomz: ; modifies edx, eax rdtsc ret randomz: ; ecx=range transparent push ebx push edx mov ebx,eax rdtsc xor ebx,eax push ecx mov cx,bx rol eax,cl mov cl,ch ror eax,cl pop ecx mul ecx mov eax,edx pop edx pop ebx ret putpixel320x200x256: ; al=color, bx=x, cx=y push es push $A000 pop es mov di,cx ; y shl cx,2 add di,cx ; 5y shl di,6 ; 320y add di,bx stosb pop es ret bkeycheck: ;returns: AH = BIOS scan code AL = ASCII character note: enhanced mov ah,0x11 ;Return: ZF set if no keystroke available, ZF clear if keystroke available int 0x16 ;only checks buffer without removing key ret set320x200x256: mov ax,$13 int $10 ret set80x25t: mov ax,$03 int $10 ret i tend to see black lines in my randomz function, so i scrambled it even more, the one below seems totally random to me, i think i can't make it any better, if you want better than this, you might wanna set up huge 3d vector tables and make large amount of calculations, here it is: Code: org 256 call set320x200x256 call initrandomz mainloop: mov ecx,319 call randomz2 mov bx,ax mov ecx,199 call randomz2 mov cx,ax push ecx mov ecx,255 call randomz2 pop ecx call putpixel320x200x256 ; al=color bx=x cx=y push eax call bkeycheck cmp al,27 pop eax jnz mainloop call set80x25t int 20h initrandomz: ; modifies edx, eax rdtsc ret randomz2: ; Z2 ecx=range transparent push ebx push edx mov ebx,eax rdtsc xor ebx,eax push ecx mov cx,bx rol eax,cl push cx and cx,$f btc ax,cx pop cx mov cl,ch ror eax,cl and cx,$f btc ax,cx pop ecx mul ecx mov eax,edx pop edx pop ebx ret putpixel320x200x256: ; al=color, bx=x, cx=y push es push $A000 pop es mov di,cx ; y shl cx,2 add di,cx ; 5y shl di,6 ; 320y add di,bx stosb pop es ret bkeycheck: ;returns: AH = BIOS scan code AL = ASCII character note: enhanced mov ah,0x11 ;Return: ZF set if no keystroke available, ZF clear if keystroke available int 0x16 ;only checks buffer without removing key ret set320x200x256: mov ax,$13 int $10 ret set80x25t: mov ax,$03 int $10 ret MATRIX 

15 Sep 2004, 23:25 

Matrix
Is there someone who likes my random generator?
If someone needs it i could make it run under 386. its a little slower and bigger then rdtsc. MATRIX 

19 Sep 2004, 17:57 

Goto page 1, 2 Next < Last Thread  Next Thread > 
Forum Rules:

Copyright © 19992020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.
Website powered by rwasa.