flat assembler
Message board for the users of flat assembler.

 Index > Projects and Ideas > RANDOM Goto page Previous  1, 2, 3, 4  Next
Author
Borsuc

Joined: 29 Dec 2005
Posts: 2466
Location: Bucharest, Romania
Borsuc
xorshift

Here some asm code (by UCM in another thread):
Code:
```;xorshift a,b,c,d
;generate random 32-bit number based on seeds a,b,c and d
;returns new seeds a,b,c,d in eax,ebx,ecx,edx respectively
;edx is usable as random result
;proc    xorshift,.a,.b,.c,.d
xorshift:
.a      equ esp+4
.b      equ esp+8
.c      equ esp+12
.d      equ esp+16
mov     eax,[esp+4]
shl     eax,15
xor     eax,[esp+4]
mov     edx,[esp+16]
shr     edx,21
xor     edx,[esp+16]
mov     ebx,eax
shr     eax,4
mov     ecx,[esp+16]
xor     eax,ebx
mov     ebx,[esp+12]
xor     edx,eax
mov     eax,[esp+8]
ret     16    ```
Of course this is only an example, you need not necessarily make this a function (you can 'inline' it).

doesn't need multiplications
28 Dec 2007, 17:11
edfed

Joined: 20 Feb 2006
Posts: 4246
Location: 2018
edfed
to slow...
28 Dec 2007, 17:15
Borsuc

Joined: 29 Dec 2005
Posts: 2466
Location: Bucharest, Romania
Borsuc
but it has a long period (128-bits used for seed).
you can use a faster, smaller period too, just one memory read (and that is the parameter! you may not need it if you're 'inlining' the function).
28 Dec 2007, 17:24
edfed

Joined: 20 Feb 2006
Posts: 4246
Location: 2018
edfed
Quote:
It makes we wonder if nature tries to communicate with us?

really wonder!
what can be the randomised methods the nature used to communicate with us?
because nature is the queen of random, she makes so many tries that now, we are all alive, human being exists because nature tries to make something that can walk on two legs and catch objects with hands...
we are the result of MILLION of years of random tries.
before us, there was dinosaur. very strange creatures.
they was randomised no?
or created to make some experiments...?
what is the code behind all these things???????????
what is this fuckin code?
i doubt it's xor, ror, not, neg or any asm instructions.
human is result of randomisation..
and more deeper, big bang is result of an other randomisation????
19 Jan 2008, 02:16
LocoDelAssembly

Joined: 06 May 2005
Posts: 4633
Location: Argentina
LocoDelAssembly
With so many philosophical posts I wonder if this thread is worth be kept at "Project and Ideas" forum
19 Jan 2008, 03:20
edfed

Joined: 20 Feb 2006
Posts: 4246
Location: 2018
edfed
look at this, it makes some various randoms, and the first time i saw the effect of this very simple algorythm, i was doubting on the reality of the life.

please, can you post some other algorytms to make a big compilation of strange random algorythms.

this will find applications in various funy programs..
there are already the 256b demo site, but i don't know any program that contain all these effects.

some good idea is to make a sort of 3D engine textured with only things like this..
result can be very interresting and funny...

19 Jan 2008, 03:33
bitRAKE

Joined: 21 Jul 2003
Posts: 3067
Location: vpcmipstrm
bitRAKE
If we have a pixel it is surrounded by eight other pixels:
Code:
```???
?1?
???    ```
We could call these eight pixels the environment of pixel A, and represent this environment with a single byte. Pixel A can perform 256 different actions based on this environment.

2. Take action
2a. change state: 0 or 1
2aa. change state of environment pixel: 0-7
2b. move pixel: 0 or 1
2bb. move active pixel: 0-7

It is rather easy to create a data driven algorithm for this:

;# get environment in AL
xlatb
;# change state?
;# move?

Randomly create blocks of 256 bytes, and a start position for pixel. If pixel doesn't do anything after some time - try again. Play with several on screen at once - different color planes. Could wrap screen or only keep the ones that stay on screen.

http://www.quinapalus.com/wi-index.html
http://www.ramos.nl/yyfire.html
http://mirekw.com/ca/index.html

Not random, the dynamics consist of both a fitness landscape and the water that rolls up that hill - figuratively speaking. Nature does not distinguish between code and data - everything executes - no programs are a mistake or error!

The code we see has no doubt cascaded from simplier times. I don't believe in truely random - some things are just beyond computablity.

[I've been very ill the last week and how the body responds is truely fastinating. A coordinated effort from many almost separate systems to erradicate a virtually unknown entity. It is a tricky thing to help the body do its work without also helping the virus.]
19 Jan 2008, 19:27
edfed

Joined: 20 Feb 2006
Posts: 4246
Location: 2018
edfed
[I've been very ill the last week and how the body responds is truely fastinating. A coordinated effort from many almost separate systems to erradicate a virtually unknown entity. It is a tricky thing to help the body do its work without also helping the virus.]

you're repaired now?
about illness, i've seen a very strange thing with a virus.
i was in bed, completelly halucinating cause i felt the millions of virii cells in my body, and with my mind, i told to all my body to fight against these virii, and it works.

since this date, i'm never ill.
approximately 8 years.
19 Jan 2008, 19:51
bitRAKE

Joined: 21 Jul 2003
Posts: 3067
Location: vpcmipstrm
bitRAKE
[I rarely get sick - once every couple years. Today I am able to eat something without it being rejected. My head is not clear - which makes coding difficult. My daughter got sick first and I about 36 hours after. It feels good to be regaining strength.]
21 Jan 2008, 00:10
edfed

Joined: 20 Feb 2006
Posts: 4246
Location: 2018
edfed
illness is always a sad experience.

and it randomly appears.

i'm affraid, probability i get sick increase with the time i'm not...

or i have my quota, i was always ill when i was child, approximatelly 4 - 5 times a year...
i hope you'll can soon code an other random algorithm...it's not so difficult to do... but one little chalenge, make it like a function that we can call without any initialisation.
21 Jan 2008, 02:09
bitRAKE

Joined: 21 Jul 2003
Posts: 3067
Location: vpcmipstrm
bitRAKE
Let me find the code for Wolfram's Rule 30.

Sorry, it is MASM syntax:
Code:
```RULE_PAINT MACRO
LOCAL _0, _1, _2, _3, _4
push    ebx
;; pointer to DIB bits
mov     esi, bi2_bits

;; skip first row
mov     ecx, bi2.biWidth
shr     ecx, 2+3 ; bits to DWORDs
lea     edi, [esi + ecx*4]

;; build all rows from first...
;  neg     ecx
push    bi2.biHeight
push    ecx
inc     DWORD PTR [esp+4]

movzx   eax, BYTE PTR [ebx].rule.rule
; all four bytes are same!
mov     edx, 01010101h
mul     edx
mov     ebx, eax

_1: mov     ecx, [esp] ; dwords per line
lea     esi, [esi+ecx*4]
lea     edi, [edi+ecx*4]
neg     ecx

mov     ebp, [esi + ecx*4]
bswap   ebp
mov     edx, ebp ; ?
rol     edx, 2
;     xor     edx, edx ; ?
inc     ecx
_3:
; edx = ...| -2 | -1 | 0
shr     edx, 2          ; get bit -1
rcl     ebp, 3          ; 3 |...| 31 | -1 | 0 | 1
; save bit 2
adc     edx, edx        ; ? |...| ? | 2

; rule bit 0
bt      ebx, ebp
rcl     eax, 1

; put bit 2 back
rcr     edx, 1          ; ? |...| ?
rcr     ebp, 3          ; 0 | 1 | 2 | ... | 31
rol     ebp, 3          ; 3 |...| 31 | 0 | 1 | 2

REPEAT 32-3
; rule bit 1 thru 29
bt      ebx, ebp
rcl     eax, 1
rol     ebp, 1
ENDM
bt      ebx, ebp
rcl     eax, 1

mov     edx, [esi + ecx*4]
bswap   edx

shld    ebp, edx, 1
; rule bit 31
bt      ebx, ebp
rcl     eax, 1

xchg    edx, ebp

bswap   eax
mov     [edi + ecx*4 - 4], eax

inc     ecx
jne     _3

inc     DWORD PTR [esp+4]
jne     _1

pop     ebx
ENDM    ```
21 Jan 2008, 02:55
FrozenKnight

Joined: 24 Jun 2005
Posts: 128
FrozenKnight
i wrote this a while back to generate large numbers of random numbers.
the r4d6m1 function is based on a statistical probably when you roll 4 dice and drop the lowest roll. it takes one parameter which is a 32 bit random or generated number in eax. (i made it for a friends D&D game. but decided to leave it there for posiable later use in rpg's LOL)

Code:
```;my implementation of the
;Mersenne Twister Random algorithm
;by FrozenKnight

;call mt_init with a seed value to set up random
;call mt_random to get random value (note if you dont set it up it will set it's self up)
;also note that it uses shared memory so that the algorthim doesnt require to be set up a second time
;when mupital applications are using it.

format PE GUI 4.0 DLL
entry  DllStart

dd 0,\                        ;Characteristics
45984F00h                ;TimeDateStamp
dw 0,\                       ;MajorVersion
1                   ;MinorVersion
dd RVA _szDllName,\   ;Name
100,\              ;Base
5,\                        ;NumberOfFunctions
5,\                   ;NumberOfNames

_FuncAddrs:    ;must match sorted according to ordeal order
dd RVA MTRand
dd RVA MTinitMan;MTinit
dd RVA rand
dd RVA srand
dd RVA r4d6m1

dd RVA _szMTinit
dd RVA _szMTRand
dd RVA _szr4d6m1
dd RVA _szrand
dd RVA _szsrand

_OrdinalsAddrs: ;must start at 0 and count up
dw 1               ;must match NameAddr order
dw 0
dw 4
dw 2
dw 3

_szDllName    db 'RAND.DLL', 0
_szMTRand db 'MTrand',0
_szMTinit    db 'MTinit',0
_szr4d6m1    db 'r4d6m1',0
_szrand      db 'rand',0
_szsrand       db 'srand',0

align 8
DllStart:
;  call MTRand
;  call MTRand
;  call MTRand
;  call MTRand
retn 4

align 8
srand:
pop  eax
pop    [_seed]
jmp        eax

;this is a simple linear random function Primes gatehred from the rand function provided by the MSVC compiler
align 8
rand:
mov eax, [_seed]
mov edx, 214013
mul edx
shr eax, 16
and eax, 7FFFh
mov [_seed], eax
ret

db 'FrozenKnight - If you use this please give me credit. ',\
'i worked hard to get this to execute as fast as i could. Thank You.', 0

align 8
MTinitMan:
mov         dword [mt_location], 623
align 8
MTinit:
mov     [mt_initlized], 1
mov      eax, [esp+4]
mov   [mt_buffer], eax
push      esi
push   ecx
push   edx
mov    edx, 1
mov         eax, mt_buffer
@@:
mov      ecx, [eax]
mov     esi, ecx
shr       esi, 1Eh
xor       esi, ecx
imul      esi, 6C078965h
mov       [eax+4], esi
inc         edx
cmp    eax, mt_buffer + (623*4)
jl        @b
pop     edx
pop    ecx
pop    esi
retn   4

align 8  ;alignments used to speed up calls and jumps.
MTRand:
push        ebx
push   edx
mov    ebx, mt_location
dec       dword [ebx]
js     .reseed
.return:
mov       eax, [ebx]
;---interlock---
mov       eax, [mt_buffer+eax*4]
;---interlock---
mov   ebx, eax
;---interlock---removed by changeing following ebx to eax

sar    eax, 0Bh
;---interlock---
xor         eax, ebx
;---interlock---
mov         ebx, eax

shl   eax, 7
;---interlock---
;---interlock---
xor      eax, ebx
;---interlock---
mov         ebx, eax

shl   eax, 0Fh
;---interlock---
and         eax, 0FFFFDF8Ch; 0EFC60000h
;---interlock---
xor      eax, ebx
;---interlock---
mov         ebx, eax

sar   eax, 12h
;---interlock---
xor         eax, ebx

pop   edx
pop    ebx
retn

align 8
.init:       ;i use a neat trick here that allows me to save
rdtsc      ;a few cycles when calling these functions by allowing
push      eax       ;the ruteen to return from it's call at a diffrent location
push        .return
jmp        MTinit
align 8
.reseed:
mov     eax, [mt_initlized]
mov    dword [ebx], 623
test      eax, eax
jz        .init

push     edi
push   ebx
push   ecx
mov    edi, mt_buffer
mov         ebx, mt_lookup
push        dword [edi]
.loop:
cmp     edi, mt_buffer + (622 * 4)
mov     ecx, [edi]
jg      .final
mov         edx, [edi+4]
and   ecx, 80000000h
and         edx, 7FFFFFFFh
or          edx, ecx
mov       ecx, edx
mov       eax, [ebx]
and     edx, 1
shr         ecx, 1
lea         edi, [edi+4]
xor   ecx, [eax]
lea     ebx, [ebx+4]
xor   ecx, [mag01+edx*4]
mov     [edi-4], ecx
jmp   .loop
align 8
.final:
pop       edx
and    eax, 80000000h
and         edx, 7FFFFFFFh
or          edx, eax
mov       eax, [ebx]
mov     ecx, edx
and       edx, 1
shr         ecx, 1
;---interlock---
xor   ecx, [mt_buffer + 396*4]
;---interlock---
xor         ecx, [mag01+edx*4]
mov     [edi], ecx
pop     ecx
pop    ebx
pop    edi
jmp    .return

r4d6m1:
push  ecx
mov    ecx, [esp + 4*2]
and       ecx, 0FFFFh
mov    eax, 18
cmp        ecx, 64473
jg      .exit
dec  eax
cmp    ecx, 61742
jg      .exit
dec  eax
cmp    ecx, 56989
jg      .exit
dec  eax
cmp    ecx, 50365
jg      .exit
dec  eax
cmp    ecx, 42274
jg      .exit
dec  eax
cmp    ecx, 33577
jg      .exit
dec  eax
cmp    ecx, 25132
jg      .exit
dec  eax
cmp    ecx, 17648
jg      .exit
dec  eax
cmp    ecx, 11479
jg      .exit
dec  eax
cmp    ecx, 6877
jg       .exit
dec  eax
cmp    ecx, 3742
jg       .exit
dec  eax
cmp    ecx, 1820
jg       .exit
dec  eax
cmp    ecx, 759
jg        .exit
dec  eax
cmp    ecx, 253
jg   .exit
dec       eax
cmp    ecx, 51
jg .exit
dec  eax
.exit:
pop      ecx
ret    4*1

section '.rdata' data readable ;writeable shareable
;N = 624
;M = 397
mt_lookup: ;lookup table has a list of addresses to seed locations this allows for faster iterations.
rept 227 kk
{
dd ((kk-1)+397)*4+mt_buffer
}
rept 397 kk
{
dd (kk-1)*4+mt_buffer
}
; rolllookup:
;  dd 64473, 18
;  dd 61742, 17
;  dd 56989, 16
;  dd 50365, 15
;  dd 42274, 14
;  dd 33577, 13
;  dd 25132, 12
;  dd 17648, 11
;  dd 11479, 10
;  dd 6877,  9
;  dd 3742,  8
;  dd 1820,  7
;  dd 759,   6
;  dd 253,   5
;  dd 51,    4
;  dd 0,     3

section '.data' data readable writeable shareable

mag01     dd  0
dd  09908B0DFh

mt_initlized    dd  0
mt_location  dd  0 ;start at 0 location

mt_buffer    dd    624 dup (?) ;mt seed values

_seed        dd    ? ;MS rand seed values

section '.reloc' fixups data discardable    ```

i have used several optimizations to try to get the best possible speed from the algorithm. i may be able to optimize it further as i wrote it quite a while ago. note some calls may be much faster than others; as the algorithm has to re seed every 624 calls. you do not need to provide a seed to begin using this algorithm as i coded it to self seed if necessary.
21 Feb 2008, 07:12
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

 Jump to: Select a forum Official----------------AssemblyPeripheria General----------------MainTutorials and ExamplesDOSWindowsLinuxUnixMenuetOS Specific----------------MacroinstructionsOS ConstructionIDE DevelopmentProjects and IdeasNon-x86 architecturesHigh Level LanguagesProgramming Language DesignCompiler Internals Other----------------FeedbackHeapTest Area
Goto page Previous  1, 2, 3, 4  Next

Forum Rules:
 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot vote in polls in this forumYou cannot attach files in this forumYou can download files in this forum