flat assembler
Message board for the users of flat assembler.

Index > Projects and Ideas > RANDOM

Goto page 1, 2, 3, 4  Next
Author
Thread Post new topic Reply to topic
edfed



Joined: 20 Feb 2006
Posts: 4347
Location: Now
edfed 26 Jun 2007, 22:38
we need the RANDOM function
call rand.32
call rand.16
call rand.8
call rand.fp
the random number is given in EAX for rand.32,in AX for rand.16,in AL for rand.8 and in a memory location for rand.fp

alone it will be slow

so i launch a participative contest for the rand.XX function


rand:
;eax isn't predifined
.32:
mov eax,[ax]
and eax,ebx
mov eax,[ax]
xor ecx,eax
mov eax,[cx]
ret
.16:
etc....


in my opinion it is possible to generate a real random generator whithout looping
like a noise
Post 26 Jun 2007, 22:38
View user's profile Send private message Visit poster's website Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4347
Location: Now
edfed 17 Oct 2007, 00:20
this is a tread about random numbers

http://board.flatassembler.net/topic.php?t=1517&start=20

but having a good random generator is hard

it needs to be really fast

and i think that it needs to be in the timer interrupt
Post 17 Oct 2007, 00:20
View user's profile Send private message Visit poster's website Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4347
Location: Now
edfed 17 Oct 2007, 01:59
Code:
;me too i can put some (c) in my code
;why don't i make that?
;because i am for the free open source without any license
;this PROGRAM is totally free to use 
;for all purpose
;no (c), no GPL, only enjoy asm coding and sharing
;fuck off the (c) and the (r) 
;totally free 
org 100h       ;were is the program in CodeSegment
mov ax,13h     ;set bios mode 13h(320*200 8bits palette RVB 18bits)
int 10h
mov ax,0a000h  ;direct to screen segment
mov es,ax
mov ax,09000h  ;screen buffer segment
mov fs,ax
jmp debut      ;jmp to main loop
;;;;;;;;;;;;;;;;here is a random generator;;;;;;;;;;;;
;;;;;;;;;;need some optimisations but it works;;;;;;;;
;if eax alway = 0 before call it, then it don't works;
rand:
call @f
mov eax,[esi]
ret
rndarray:
rb 4*4
@@:
        or eax,1  ;now, even if eax is always zero, it works
        mov esi,rndarray   ;strange but it works
        add eax,[esi]
        add eax,[esi+4]
        add eax,[esi+8]
        add eax,[esi+12]
        mov [esi],eax
        add eax,[esi+1]
        mov [esi+2],eax
        add eax,[esi+3]
        mov [esi+4],eax
        add eax,[esi+5]
        mov [esi+6],eax
        add eax,[esi+7]
        mov [esi+8],eax
        add eax,[esi+1]
        mov [esi+9],eax
        add eax,[esi+3]
        mov [esi+10],eax
        add eax,[esi+5]
        mov [esi+11],eax
        add eax,[esi+7]
        mov [esi+12],eax
        ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
x rd 1
y rd 1
iterates dd 10000
include 'scancode.inc'
debut:
        mov ecx,[iterates]
@@:
        call rand
        mov [x],eax         ;X=rnd (32)
        call rand
        mov [y],eax         ;Y=rnd (32)
        mov edi,[y]
        imul edi,320
        add edi,[x]         ;compute the pixel position
        call rand           ;color=rnd (32) [0-3] + 16
        and edi,0ffffh      ;limit edi to 65535
        and al,0fh          ;limit to 16 colors
        add al,16           ;center the color on grey scale
        mov [fs:edi],al     ;write color at XY on buffer
        loop @b             ; iterate
        mov edi,320*200-1
        xor ah,ah
@@:
        mov al,[fs:edi]     ;copy buffer
        mov [es:edi],al     ;on screen
        mov [fs:edi],ah     ;and clear it
        dec edi
        jnl @b
        mov ax,0c00h        ;clear keyboard bios buffer
        int 21h             ;to avoid the beeps
        in al,60h           ;read current scancode
        cmp al,1;key.echap
        je exit
        cmp al,4bh;key.left
        jne @f
        shr [iterates],1
        jne @f
        inc [iterates]
        @@:
        cmp al,4dh;key.right
        jne @f
        add [iterates],100
@@:
        jmp debut           ;jmp main loop
exit:                       ;exit
        ret                 ;a simple ret works, no?       
    


Last edited by edfed on 20 Oct 2007, 12:33; edited 1 time in total
Post 17 Oct 2007, 01:59
View user's profile Send private message Visit poster's website Reply with quote
Dex4u



Joined: 08 Feb 2005
Posts: 1601
Location: web
Dex4u 17 Oct 2007, 02:56
Code:
call    Randcall    Rand; some more code here;----------------------------------;Our random number procRand:                                 mov     ax,[seed]             ;Move our number into AXimul    [k1]                  ;This mutiply seed has -numberadd     ax,[k2]               ;Adds k2 to axmov     [seed],ax             ;Moves AX into seedror     al,1                  ;Rotate right 1rol     ah,1                  ;Rotate left 1retseed dw 0k1 dw 9821k2 dw 1    

You will need something to fill seed on start, like timer etc.
Post 17 Oct 2007, 02:56
View user's profile Send private message Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4347
Location: Now
edfed 17 Oct 2007, 13:49
sorry Dex!
i've tried your rand
it's not really random

it have some values that appears at fixed intervals
for exemple 0 is frequently present
and a random generator for asm don't need the timer also

sorry
try again

my rand is better but slower Sad

try your rand with my code
and compare with my rand you'll see the difference

thank you a lot to have interrest in my topics Wink
Post 17 Oct 2007, 13:49
View user's profile Send private message Visit poster's website Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 17 Oct 2007, 18:50
edfed: your code isn't random either. It uses values of register as seed, which often are fixed when calling random. You NEED something like timer (or soundcard noise, or something like that) to get some random seed.

Windows provides cryptography API for good random values.
Post 17 Oct 2007, 18:50
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
Dex4u



Joined: 08 Feb 2005
Posts: 1601
Location: web
Dex4u 17 Oct 2007, 18:52
That ok, it just some code from a old game i made, but i did it from my head and got it a little wrong, here's the right code, i do not know if it works any better ?
Code:
call    Randmov  [seed],ax  <MIST THIScall    Rand   ;THE OUT put of this should be random; some more code here;----------------------------------;Our random number procRand:                                 mov     ax,[seed]             ;Move our number into AXimul    [k1]                  ;This mutiply seed has -numberadd     ax,[k2]               ;Adds k2 to axmov     [seed],ax             ;Moves AX into seedror     al,1                  ;Rotate right 1rol     ah,1                  ;Rotate left 1retseed dw 0k1 dw 9821k2 dw 1    


Here a screenshot of the game, the rand is to put the stars at random placer.
They look random to me Wink
http://www.dex4u.com/clipong.htm

PS: No computer can generate true random numbers.


Last edited by Dex4u on 18 Oct 2007, 20:11; edited 1 time in total
Post 17 Oct 2007, 18:52
View user's profile Send private message Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4347
Location: Now
edfed 17 Oct 2007, 19:40
PS: No computer can generate true random numbers.

it's wrong

and it don't need seed
it needs many seeds
and the random is effectively a noise

probability for all values is the same

if some values appears frequently and others never
so it's not random

and if you want a good seed, just use the random state of the registers
before a rand call
plus a circular table
or a strange table like in my code

you'll see it is random

mov [seed],ax???
call rand
mov ax,[seed]????
why?

to test a random generator, i have a solution
for exemple in 8 bits
256 values
with bargraph to show the times a value is generated

and with a variable algorythm it is really possible to obtain real random
but it needs to have imagination not only science Wink
Post 17 Oct 2007, 19:40
View user's profile Send private message Visit poster's website Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 17 Oct 2007, 20:20
Quote:
and if you want a good seed, just use the random state of the registers

according to this, "nop" is random number generator too. It takes "random state" of EAX register, and returns it in EAX.

get serious...
Post 17 Oct 2007, 20:20
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4347
Location: Now
edfed 17 Oct 2007, 22:16
no, nop is not a real instruction
it only increments IP
thats all
rdtsc is a good random source
but only for ring 1 code:(
Post 17 Oct 2007, 22:16
View user's profile Send private message Visit poster's website Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 18 Oct 2007, 17:59
Quote:
no, nop is not a real instruction
it only increments IP

okay, then "neg ebx" is a good random number generator, it takes "random state" of EAX register, and returns it in EAX.

by the way, define "real instruction" please Smile

Quote:
rdtsc is a good random source

rdtsc is a one-time good source, for random seed or something. it is not good as a repeated source of random number.

Quote:
but only for ring 1 code:(

bullshit. there is no ring1 code in any modern operating system, and RDTSC works in ring3 code in every modern operating system i know.
Post 18 Oct 2007, 17:59
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4347
Location: Now
edfed 18 Oct 2007, 20:09
instructions that have an effect in the function are real instructions

hey vid ! do you know the transfert functions
the differents orders?
1 2 3 4 5
cauer
bessel
tchébychev
dirac
fournier
????
with their studes you can conclude that in a quantified universe like in pc you will obtain an unusuable band over Fs/2
and that white noise is the presence of all frequencies at same value
a flat spectrum*

this flat spectrum can be achieved by a various value

my algorythm is able to achieve a random state

show me that it isn't and prttttt
rdtsc is not eccessible with win98
and it is modern cause it supports modern hard and soft ware
Post 18 Oct 2007, 20:09
View user's profile Send private message Visit poster's website Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 18 Oct 2007, 22:50
Quote:
hey vid ! do you know the transfert functions
the differents orders?
1 2 3 4 5
cauer
bessel
tchébychev
dirac
fournier

only very little...

Quote:
with their studes you can conclude that in a quantified universe like in pc you will obtain an unusuable band over Fs/2
and that white noise is the presence of all frequencies at same value
a flat spectrum*

though i don't know theory, you are the one saying you can use state current value of register as source of randomness.

Quote:
my algorythm is able to achieve a random state
show me that it isn't and prttttt

simply: code that has predictable (constant for example) value in all regters, and calls your function. returned value would be always same, which is not really the best random possible
Post 18 Oct 2007, 22:50
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4347
Location: Now
edfed 19 Oct 2007, 14:19
current value of register is not the seed
it's only a little part

a circular array is the other part

and accessing this array in a strange way like
Code:
;;;;;;;;;;;;;;;;here is a random generator;;;;;;;;;;;;
;;;;;;;;;;need some optimisations but it works;;;;;;;;
;no (c) no GPL no licence
;make what you what with that code
rand:
call @f
mov eax,[esi]
ret
rndarray:
rb 4*4
@@:
        or eax,1  ;now, even if eax is always zero, it works
        mov esi,rndarray   ;strange but it works
        add eax,[esi]
        add eax,[esi+4]
        add eax,[esi+8]
        add eax,[esi+12]
        mov [esi],eax
        add eax,[esi+1]
        mov [esi+2],eax
        add eax,[esi+3]
        mov [esi+4],eax
        add eax,[esi+5]
        mov [esi+6],eax
        add eax,[esi+7]
        mov [esi+8],eax
        add eax,[esi+1]
        mov [esi+9],eax
        add eax,[esi+3]
        mov [esi+10],eax
        add eax,[esi+5]
        mov [esi+11],eax
        add eax,[esi+7]
        mov [esi+12],eax
        ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    

makes the state of random number very hard to predict

and with the variable state of eax, we obtain something with no loop
eax can be zero or not, it's not important because of the or eax,1


Last edited by edfed on 20 Oct 2007, 12:36; edited 1 time in total
Post 19 Oct 2007, 14:19
View user's profile Send private message Visit poster's website Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4046
Location: vpcmpistri
bitRAKE 19 Oct 2007, 17:53
Here is an interesting random number generator I stumble across recently:

http://home.southernct.edu/~pasqualonia1/ca/report.html

The code should be easy to convert to FASM. It's very fast for the level of randomness. If you need many (millions per second) random numbers then this is what you want.


Description: Cellular Automata PRNG (MASM source module).
Download
Filename: PRNG_CA_Pasqualoni.rar
Filesize: 4.01 KB
Downloaded: 851 Time(s)

Post 19 Oct 2007, 17:53
View user's profile Send private message Visit poster's website Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4347
Location: Now
edfed 20 Oct 2007, 12:17
really MASM is very shitty
mov BYTE [PRNG_CA_Pasqualoni_State][PRNG_CA_Pasqualoni_State_Width][ebp][4-4]
it's not the simple manner to write asm code

unreadable
now i understand why there is high level languages

if asm is that(MASM)
i understand everything

why programmers don't use asm etc etc ...
because the syntax of MASM is very a big shit
and it's very hard to translate in fasm
FASM is really the best

and my algorythm is able to generate many more numbers than the (c) PRNG_CA_Pequinol (c) GPL licence 2006
just need to be tested by a random analyser
to know the randomness of it

simple to use
simple syntax
explicit

THANK YOU VERY MUCH MR GRYSZTAR TOMASZ

eg: my translation doesn't work for the moment
it is for a single segment model because i don't understand why it is in multisegment model
it's only little function

NOTA:
a lot of random generators are possible
a lot of method are possible too
the PRNG_CA_etc etc ... like method is one that i imagine in 2005
but too much slow and dirty so i never code it

my generator is slow too so i will optimise it one day
but random is not the priority


Description:
Download
Filename: PP.asm
Filesize: 3.09 KB
Downloaded: 870 Time(s)

Post 20 Oct 2007, 12:17
View user's profile Send private message Visit poster's website Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4046
Location: vpcmpistri
bitRAKE 20 Oct 2007, 20:21
edfed wrote:
really MASM is very shitty
mov BYTE [PRNG_CA_Pasqualoni_State][PRNG_CA_Pasqualoni_State_Width][ebp][4-4]
it's not the simple manner to write asm code
Laughing I have this strange way of documenting my thought process within the instruction - it is not very useful to others I suppose.

The fact of the matter is that it indexes from the end of array.

Your translation is good. Very Happy Although, I'd put the ALIGN 64 before PPRULE.
Post 20 Oct 2007, 20:21
View user's profile Send private message Visit poster's website Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4347
Location: Now
edfed 21 Oct 2007, 20:27
ok


i want to make a poly algorythm random
each sub random need to have the same i/o effect

to complies with

Code:
rand dd 0
dd @f-$-4
dd rand1
dd rand2
dd randvid
dd rand3
....
@@:
rand1:
include 'random1.inc'
rand2
include 'random2.inc'
randvid:
nop
mov eax,eax
ret
rand3:
...

macro RND 
{
mov ebx,[rand]
mov ebx,[rand+8+ebx]
call [ebx]
mod eax,[rand+4]   ; macro that returns the mod in eax instead of idiv 
mov [rand],eax
mov ebx,[rand+8+eax]
call ebx 
}
;returns a random dword in eax
    


this random generator will be able to manage many algorythms and then really random
Post 21 Oct 2007, 20:27
View user's profile Send private message Visit poster's website Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4046
Location: vpcmpistri
bitRAKE 21 Oct 2007, 23:52
For me psuedo-random is better, because I might want to return to complex calculations by just storing the state of random number generator. We can never return to "truely" random place without storing the whole universe (maybe, just all of memory for now).

Additionally, I really need 10^8 PRN's per second without poluting the (data/instruction) cache too much. Given these constraints Pasqualoni's automata is quite impressive. It is not thread safe, but I'll just use a different state array for each thread.

I have always loved FASM - from the first day I saw it.
Post 21 Oct 2007, 23:52
View user's profile Send private message Visit poster's website Reply with quote
asmfan



Joined: 11 Aug 2006
Posts: 392
Location: Russian
asmfan 22 Oct 2007, 08:36
Random numbers, based on pseudo rand.num. as an idea - random byte/word/dword value of pseudo random sector (or file) of HDD or smthg like this. /P.S. my HDD is really "truely" random place polluted with different sources:)/
Post 22 Oct 2007, 08:36
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page 1, 2, 3, 4  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-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.