Page 1 of 1

radom int generator

Posted: Wed Aug 27, 2008 8:07 pm
by chezzestix
Im trying to create a random int generator that chooses a number 1-4 for one of my current projects.

Code: Select all

define SINCE_MIDNIGHT 0x406C   //the address of since midnight ticks as found on a website
//Im assuming the bios keeps up with this and I dont need to call anything to gain access to this
start_make_random:  //I've also tried 0x407F to get the last byte but that gives the same results
mov al,[SINCE_MIDNIGHT]
make_random:
sub al,4
cmp al,0x5
jge make_random  //basically I'm finding SINCE_MIDNIGHT%4, I cmp it to 5 so I never get 0
ret 
However this code returns a static value that changes with the divisor... whats going on here?

Re: radom int generator

Posted: Wed Aug 27, 2008 10:19 pm
by Fear
Three things:
1) Your code does not find SINCE_MIDNIGHT % 4, it finds SINCE_MIDNIGHT - 4.
2) Why bother comparing to 5? It should always return a value between 0 and 3.
3) Are you sure you are getting the value of SINCE_MIDNIGHT from the correct address and that it is only one byte?

Re: radom int generator

Posted: Thu Aug 28, 2008 12:38 am
by bewing
If you want a decent random number generator, look up Mersenne twisters on wikipedia. AFAIK, the sourcecode is public domain.

Re: radom int generator

Posted: Thu Aug 28, 2008 12:40 am
by stephenj
Try al=4.

A simpler method would be:

Code: Select all

and al, 11b
inc al
Just me wondering, approximately how many ticks are you expecting to occur between each call? Furthermore, if you have TCP/IP implemented, there are far superior sources of entropy than a tick counter.

Re: radom int generator

Posted: Thu Aug 28, 2008 6:29 pm
by chezzestix
Bah hum bug to using time as a seed.

I rewrote my code so that the user specifies a 10 character seed and then made an algorithm that does a pretty nice job at making random numbers. I dont know how random it is but within a screen of 2000 outputs I could not find a single repeat, even with a seed of all As. So its atleast good for 2000+ entries which in a 16-bit project isnt bad.

Code: Select all

rand_int:
mov si,[seed_pos]
mov al,[si]  //Stage one XORs three 
mov di,[seed_pos+2]
mov ah,[di]
xor al,ah
mov di,[seed_pos+4]
mov ah,[di]
xor al,ah
and al,3
inc al
push ax
mov di,[seed_pos]
mov al,[di]
rol al,3
mov [di],al
mov ah,al
mov di,[seed_pos+2]
mov al,[di]
xor al,ah
and ah,al
rol al,7
mov [di],al
mov di,[seed_pos+4]
mov al,[di]
xor al,ah
rol al,5
mov [di],al
add si,9
call fix_seed_pos
mov [seed_pos],si
mov si,[seed_pos+2]
add si,4
call fix_seed_pos
mov [seed_pos+2],si
mov si,[seed_pos+4]
add si,7
call fix_seed_pos
mov [seed_pos+4],si
pop ax
ret

fix_seed_pos:
cmp si,0x7C00
jl fix_seed_pos_end
sub si,0x7C00
add si,RN_BUFFER
fix_seed_pos_end:
ret

Re: radom int generator

Posted: Fri Aug 29, 2008 1:57 am
by Combuster
The trick with random numbers is not just how many different numbers you get after one another, in fact its a bad measure as I could just do return(seed++) and have an unique number everytime.

What you should be looking at is the average distribution of output (i.e. are the numbers on average evenly distributed), the local distribution of output (if I have seen the last few numbers, can the next one still be anything) and the amount of outputs you need to be able to predict the next one.

True randomness will be evenly distributed, can theoretically churn out an infinite number of zeroes after one another, and is impossible to predict.

Re: radom int generator

Posted: Fri Aug 29, 2008 8:17 am
by AndrewAPrice

Re: radom int generator

Posted: Fri Aug 29, 2008 8:21 am
by DeletedAccount
read that the Linux kernel has a random number generator based on entropy etc which sounds quite solid :D . If I remember it correctly I found it in the appendix section of Robert Lowe's book

Regards
Thomas

Re: radom int generator

Posted: Fri Aug 29, 2008 10:17 am
by chezzestix
@combuster
Im sorry what I said my not have been totally clear. I looked at a screen full of output and just as you do with a decimal to find out if its terminating or not I looked for a pattern in the output. Which in the final version I was unable to find as with previous versions.

@messiahandrw and bewing
Thanks for the link however decoding that text/algorithem would take 5x as long as making my own. Plus I can imagine the code taking up the 512 byte limit Im trying to stay within with the bootcode.

@messiahandrw and Sandeep
This isnt for an OS per-se. I am making an OS independent program which come the end of production will have to do as much as a single thread OS. I ran into a brickwall again with the production of my OS so I have taken a detour to further solidify my programming skills. This topic ties in with my Hungry Dots topic in the test request forum.

Re: radom int generator

Posted: Fri Aug 29, 2008 7:47 pm
by bewing
OK, I didn't realize you were trying to stuff an entire random number generator (a mathematically complex algorithm!) into a bootloader.

If you just want cheap-@$$ "random" numbers that take up almost no code, then you should just be reading a byte from the PIT countdown timer.

Also, you shouldn't try to live inside a 510 byte bootsector boundary. It can't be done effectively. While it is true that you learn some important things about programming ASM when trying to do it, you also learn bad habits at the same time. Overly compressed ASM code is usually fairly slow, and hard to read/maintain.

chezzestix's easy questions

Posted: Sat Aug 30, 2008 11:28 am
by chezzestix
Yea and this project is quickly expanding beyond the sector barrier..

Speaking of:
Im having some trouble getting this code to work. Its supposed to load the next sector into ram at 0x500 which the memory map says has about 30Kib of free space I would like to make use of. Whats wrong with this code?

Code: Select all

...

mov ch,0x0
mov cl,0x4
mov dh,0x0
mov dl,0x0
mov ax,0x0
mov es,ax
mov bx,0x500
mov ah,02h
mov al,0x1
int 13h
mov ah,0x0
mov al,0x13
int 10h
main:
call ddraw
jmp _end  

...

times 510-($-$$) db 0
dw 0x55AA

org 0x500

ddraw:
mov ax,(VID_MEM_START)
mov es,ax
mov bx,0x0000
mov ax,15
mov [es:bx],ax
ret

Ddestroy:
mov ax,VID_MEM_START
mov es,ax
mov bx,0x0000
mov ax,0
mov [es:bx],ax
ret

_end:

times 512-($-$$) db 0

Re: radom int generator

Posted: Sat Aug 30, 2008 11:54 am
by Fear
Does it even boot, or does whatever emulator your using yell at you? It doesn't look like you have a bootloader signature (DW 0xAA55) though you may have just cut that off by accident.

Re: radom int generator

Posted: Sat Aug 30, 2008 9:56 pm
by chezzestix
times 510-($-$$) db 0
dw 0x55AA
The ...s is where I cut the code that doesnt matter out but this is clearly in the code. It pans out to be AA55 in the hex.

Re: radom int generator

Posted: Sun Aug 31, 2008 12:52 am
by bewing
Are you sure your org for the bootsector is 0x7c00?
Why are you loading sector number 4 (CL), instead of sector number 2?

Re: radom int generator

Posted: Sun Aug 31, 2008 11:07 am
by chezzestix
.... WTH was up with that math...

It seemed right yesterday, it works now thank you very much.