flat assembler
Message board for the users of flat assembler.

Index > DOS > Random numbers

Goto page 1, 2  Next
Author
Thread Post new topic Reply to topic
OzzY



Joined: 19 Sep 2003
Posts: 1029
Location: Everywhere
OzzY
How to generate a random number between 0 and 9??

Thanks!!
Post 01 May 2004, 19:21
View user's profile Send private message Reply with quote
OzzY



Joined: 19 Sep 2003
Posts: 1029
Location: Everywhere
OzzY
Nobody knows?
Post 01 May 2004, 21:51
View user's profile Send private message Reply with quote
comrade



Joined: 16 Jun 2003
Posts: 1137
Location: Russian Federation
comrade
take current time, divide by 10, take remainder

_________________
comrade (comrade64@live.com; http://comrade.ownz.com/)
Post 01 May 2004, 22:00
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger ICQ Number Reply with quote
OzzY



Joined: 19 Sep 2003
Posts: 1029
Location: Everywhere
OzzY
Could you give me an example? =P
I'm having problems when trying to divide th number, I allways get an error...

Thanks
Post 01 May 2004, 22:25
View user's profile Send private message Reply with quote
comrade



Joined: 16 Jun 2003
Posts: 1137
Location: Russian Federation
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]    

_________________
comrade (comrade64@live.com; http://comrade.ownz.com/)
Post 01 May 2004, 22:44
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger ICQ Number Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
i suggest you remembering previously generated number and additng (or whatever) it to currently generated. This way your random number generator works even if called more frequently than time is updated.
Post 02 May 2004, 15:14
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
OzzY



Joined: 19 Sep 2003
Posts: 1029
Location: Everywhere
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
Post 02 May 2004, 17:58
View user's profile Send private message Reply with quote
Vortex



Joined: 17 Jun 2003
Posts: 318
Vortex
OzzY,

To generate random numbers, you use the rand function provided by C run-time DLLs. Check the attachment for an example.


Description: Blue colored bugs invading your desktop
Download
Filename: Bug.zip
Filesize: 2.4 KB
Downloaded: 408 Time(s)


_________________
Code it... That's all...
Post 02 May 2004, 19:09
View user's profile Send private message Visit poster's website Reply with quote
OzzY



Joined: 19 Sep 2003
Posts: 1029
Location: Everywhere
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!!
Post 02 May 2004, 19:17
View user's profile Send private message Reply with quote
neonz



Joined: 02 Aug 2003
Posts: 62
Location: Latvia
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?

Thanks


You can get time from Real-Time Clock (RTC) chip directly.
http://www.nondot.org/sabre/os/files/MiscHW/RealtimeClockFAQ.txt
Post 02 May 2004, 20:57
View user's profile Send private message Visit poster's website Reply with quote
OzzY



Joined: 19 Sep 2003
Posts: 1029
Location: Everywhere
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 ?
Post 03 May 2004, 00:18
View user's profile Send private message Reply with quote
Intrinsic



Joined: 03 Feb 2004
Posts: 13
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://www-2.cs.cmu.edu/afs/cs/user/ralf/pub/WWW/files.html ) and that'll contain all the info you need.
Post 04 May 2004, 10:13
View user's profile Send private message Reply with quote
Vion



Joined: 26 Apr 2004
Posts: 6
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 n-1 + 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(M-2) 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 (p-1) and is obtained if and only if a is primitive element modulo p "
Post 04 May 2004, 15:07
View user's profile Send private message Reply with quote
comrade



Joined: 16 Jun 2003
Posts: 1137
Location: Russian Federation
comrade

_________________
comrade (comrade64@live.com; http://comrade.ownz.com/)
Post 04 May 2004, 16:56
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger ICQ Number Reply with quote
Jaques



Joined: 07 Jun 2004
Posts: 79
Location: Everywhere
Jaques
http://www.agner.org/random/
has some good random # generators
Post 19 Jun 2004, 03:13
View user's profile Send private message Reply with quote
prino



Joined: 24 Jun 2004
Posts: 20
prino
Very short, very fast and very good:
http://www.jstatsoft.org/v08/i14/xorshift.pdf
Post 24 Jun 2004, 17:36
View user's profile Send private message Visit poster's website Reply with quote
Bitdog



Joined: 18 Jan 2004
Posts: 97
Bitdog
Nasm proc, might need converting,
returns 0-255

Code:
; ========================= RANDOM.INC =========================
; Procedure, RANDOM, generates random numbers in AL reg (not many 38's though)
; It gets every number 0-255 with no appearant pattern in even amounts
;
; Call with,      NOTHING
;
; Returns,      AL = random number between 0-255,
;          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

    
Post 05 Jul 2004, 01:25
View user's profile Send private message Reply with quote
Matrix



Joined: 04 Sep 2004
Posts: 1171
Location: Overflow
Matrix
Random numbers are more random on windows :DDD

MATRIX
Post 08 Sep 2004, 12:19
View user's profile Send private message Visit poster's website Reply with quote
Matrix



Joined: 04 Sep 2004
Posts: 1171
Location: Overflow
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
Post 15 Sep 2004, 23:25
View user's profile Send private message Visit poster's website Reply with quote
Matrix



Joined: 04 Sep 2004
Posts: 1171
Location: Overflow
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
Post 19 Sep 2004, 17:57
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page 1, 2  Next

< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.