flat assembler
Message board for the users of flat assembler.

Index > DOS > Random numbers

Goto page Previous  1, 2
Author
Thread Post new topic Reply to topic
MCD



Joined: 21 Aug 2004
Posts: 602
Location: Germany
MCD 22 Sep 2004, 19:29
The RDTSC-version is the smallest, easiest and quickest one from all.

If you want doing it with a mathematical formula, try this optimized chaotic logistic growth model formula:

Xn+1 = (4 - Xn) * Xn

That's all!

Where X0 should be between 0 and 4 and should not be an integer number

=> 0 < X0 < 4 & ! (X0 IN IntegerNumber)

I have both integer and floating point Smile) version somewhere around, but I messed them up and don't find them anymore.
Post 22 Sep 2004, 19:29
View user's profile Send private message Reply with quote
BreakDebugJeff



Joined: 22 Oct 2004
Posts: 1
Location: Ocean Shores, WA
BreakDebugJeff 09 Nov 2004, 10:51
I am new here, but I saw another user post the RDTSC instruction in this colum, and thought that I would add my two cents:

Code:
inline int randrnd(int max)
{
   int a;
   _asm
   {
      rdtsc   ; call read time-stamp counter instruction
                ; returns timer value in edx:eax
      cdq     ; convert double word to quad word
                ; edx:eax <-- sign extension of EAX
      idiv max   ; divide edx:eax by max integer
                     ; stores result in EAX <- Quotient, EDX <- Remainder
                     ; EDX = edx:eax % max
      cmp edx, 0  ; compare value to zero?
      jnl after       ; jump if not less than to after: label
      neg edx      ; otherwise, change sign to positive
after:
      mov a, edx  ; store random number in a
   }
   return(a);      // return random number
}
    


This is an inline assembly program function that works in Visual C++.NET

Let me know what you think.

Jeff Rolling Eyes Laughing Very Happy
Post 09 Nov 2004, 10:51
View user's profile Send private message Visit poster's website Reply with quote
Matrix



Joined: 04 Sep 2004
Posts: 1166
Location: Overflow
Matrix 09 Nov 2004, 12:24
Hy,
wel, it doesn't seem to be a random to me, however i 'd do this instead:

Code:
inline int randrnd(int max) 
{ 
int a; 
_asm 
{ 
rdtsc ; call read time-stamp counter instruction 
; returns timer value in edx:eax 
cdq ; convert double word to quad word 
; edx:eax <-- sign extension of EAX 
idiv max ; divide edx:eax by max integer 
; stores result in EAX <- Quotient, EDX <- Remainder 
; EDX = edx:eax % max 
or edx,edx ;cmp edx, 0 ; compare value to zero? 
jns after ;jnl after ; jump if not less than to after: label 
neg edx ; otherwise, change sign to positive 
after: 
mov a, edx ; store random number in a 
} 
return(a); // return random number 
}
    
Post 09 Nov 2004, 12:24
View user's profile Send private message Visit poster's website Reply with quote
beppe85



Joined: 23 Oct 2004
Posts: 181
beppe85 09 Nov 2004, 13:05
If you need a non-negative integer, treat it as such!

Code:
rdtsc
xor     edx, edx ; non-negative quad-word
div     max      ; hope it work so
    


Not tested, but I believe it does the same thing. Note that it returns a number 0 <= n < max, ie. the max is not the max, it's the number at the right of the real max!
Post 09 Nov 2004, 13:05
View user's profile Send private message Reply with quote
Matrix



Joined: 04 Sep 2004
Posts: 1166
Location: Overflow
Matrix 09 Nov 2004, 16:13
btw.: here 's my latest random number generator, better then the ones i wrote recently

Code:
org 256

call set320x200x256
call initrandomz


mainloop:

mov ecx,320
call randomz3
mov bx,ax

mov ecx,200
call randomz3
mov cx,ax

push ecx
mov ecx,256
call randomz3
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


randomz3: ; Z3 ecx=range transparent
push ebx  ; number returned is:  0 <= n < ecx
push edx
mov ebx,eax
rdtsc

mov dx,cx

mov cl,al
xor cl,ah
ror ch,4
xor bl,ch
and cx,$f
rol bx,cl
btc bx,cx
xor bx,ax
ror eax,16
xor bx,ax
ror eax,16
mov cl,al
xor cl,ah
ror ch,4
xor bl,ch
and cx,$f
ror bx,cl
btc bx,cx

mov cl,bl
.minorloop:
rol eax,cl
and cx,$f
btc ax,cx
loopw .minorloop
mov cl,bh
.majorloop:
ror eax,cl
and cl,$f
btc ax,cx
loopw .majorloop
mov cx,dx

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
    
Post 09 Nov 2004, 16:13
View user's profile Send private message Visit poster's website Reply with quote
Bitdog



Joined: 18 Jan 2004
Posts: 97
Bitdog 12 Nov 2004, 02:37
I tried a few random number generators, but I ended up finding one that worked by putting it in a loop (CX=5000 loops), saving & counting the random output, keeping count of how many times it returned 0, then 1, etc
0-255, then alph organize the text file output 0-255
I found that the nicest looking, intelegent, best promoted RANDOM number gen was usually the worst. They would miss numbers (lots of them)
and would have one number get an unusually high number of hits
compaired to other numbers. It's suspose to be an even amount of hits
for each number when the generator is called repeatedly alot.
Any way, since most programs that use a RAND GEN work in a loop,
which calls the generator on consistant intervals, the save/count loop
seemed to act much like a commonly used program.

Well, it's just some thoughts anyway.

Bitdog
Post 12 Nov 2004, 02:37
View user's profile Send private message Reply with quote
Matrix



Joined: 04 Sep 2004
Posts: 1166
Location: Overflow
Matrix 12 Nov 2004, 03:09
Bitdog wrote:
I tried a few random number generators, but I ended up finding one that worked by putting it in a loop (CX=5000 loops), saving & counting the random output, keeping count of how many times it returned 0, then 1, etc
0-255, then alph organize the text file output 0-255
I found that the nicest looking, intelegent, best promoted RANDOM number gen was usually the worst. They would miss numbers (lots of them)
and would have one number get an unusually high number of hits
compaired to other numbers. It's suspose to be an even amount of hits
for each number when the generator is called repeatedly alot.
Any way, since most programs that use a RAND GEN work in a loop,
which calls the generator on consistant intervals, the save/count loop
seemed to act much like a commonly used program.

Well, it's just some thoughts anyway.

Bitdog


Bitdog,
is it air you breathing? Smile

somtimes things are not what they seem to be.
Post 12 Nov 2004, 03:09
View user's profile Send private message Visit poster's website Reply with quote
Bitdog



Joined: 18 Jan 2004
Posts: 97
Bitdog 14 Nov 2004, 16:45
So what's wrong with checking the output of a Rand Gen ?
Post 14 Nov 2004, 16:45
View user's profile Send private message Reply with quote
Matrix



Joined: 04 Sep 2004
Posts: 1166
Location: Overflow
Matrix 15 Nov 2004, 01:37
Nothing wrong , test it excessively.
i tried to plot random xmax ymax color pixels to 1024x768 24 bit and it looked random, and no white spots or somethin'
so its great for me. and it has MIPSSS!!
not like storing large arrays of previus numbers and doing whit whatever matrix transformations.
Post 15 Nov 2004, 01:37
View user's profile Send private message Visit poster's website Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4354
Location: Now
edfed 17 Oct 2007, 00:17
how can we use these random generators to have a simple function

it need to be really fast and random

it is a background program(in timer interrupt also)
and the function is called by
[code]
call rnd32
call rnd16
call rnd8
call rndfp

and all returns a value in eax,ax,al or st0

no?

one thing: MATRIX, I've tried your rnd, it is not very rnd and if ecx is too big, it crash
and it is not the faster
Post 17 Oct 2007, 00:17
View user's profile Send private message Visit poster's website Reply with quote
Matrix



Joined: 04 Sep 2004
Posts: 1166
Location: Overflow
Matrix 28 Oct 2007, 21:16
sorry for late reply,
though it won't be useful either,

i wanted to make a simple range setting option using ECX as maximum and 0 for minimum,
if you have other ideas that are better i'm interested too.

the random generated by the function is 32bit without the maximum setting at the end.

if testing it with putpixel, do not set above the limits, or the putpixel will overflow and write below the video memory buffer.
Post 28 Oct 2007, 21:16
View user's profile Send private message Visit poster's website Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4354
Location: Now
edfed 28 Oct 2007, 21:47
you just have to look at /projects and ideas/RANDOM
with many guys, we have discuss about simples randoms functions that are finally usefull for all X86 based systems

needs some (not really need ) optimisations
Post 28 Oct 2007, 21:47
View user's profile Send private message Visit poster's website Reply with quote
MCD



Joined: 21 Aug 2004
Posts: 602
Location: Germany
MCD 29 Oct 2007, 11:10
I like it. I used to use a similar one
Post 29 Oct 2007, 11:10
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page Previous  1, 2

< 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-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.