Simple question this time 
How can I create a random number. For my purpose I need random between 0 and 1 but I have no clue how to do it.
Login or register to post comments
Simple question this time 
How can I create a random number. For my purpose I need random between 0 and 1 but I have no clue how to do it.
Interesting, as in assembler you can only work with integers
Typical ways to do it: let a very fast counter count and some user interaction (press space key) determines random number (current value). For the counter you could for instance use the R register.
But I'm sure there are many people here who can provide details
Are there truly random numbers?If i use an emulator execute the code and snap at a specific time,lets suppose there was a time snapper that allowed me to pause the program at exactly 4.3545454 seconds each time,would the results be truly random?In many old games and programs i seem to think that true random generator maybe does not exist?
a very fast counter
You can use the VDP interrupt counter @ adress 0xFC9E for that, it is increased every VDP interrupt.
nikodr: no, there are no truly random numbers. Otherwise something like the reverse function and TAS wouldn't exist
Maybe something like this would do the trick for you: (Repeats after 256 values)
Rand8 ld a,Seed ; Seed is usually 0 ld b,a add a,a add a,a add a,b inc a ; another possibility is ADD A,7 ld (Rand8+1),a rlca and 1 ret
manuel: its possible to generate true random numbers using some expensive boards that use a mix of optics and quantum physics. We evaluated some of those boards back in 2005 for an OTP through SMS project...
about the Nyy code, afaik, it would Always produce the same sequence unless you use time or something else as a seed. what can be used as a seed? thAt address at the end of rAm maybe?
Yes, that was just very good example of very bad random number generator... Make it more complex and you get more complex random numbers... Anyway in the end there is no such a thing as random number available on MSX. It is always pseudorandom number.
If you need real random numbers, you can download them from internet:
http://qrng.physik.hu-berlin.de/download
If we want to play with the idea, how to get best possible random numbers, then how about if we use external clock MIDI-timer as interrupt source, then record noise from Gradiente Expert 1.0 databus and apply for example Von Neumann bias cancellation?
How to record Z80-databus on interrupt time:
; DATABUS.GEN Ver 2.1 for MSX homecomputers (Made By: NYYRIKKI) ; ; This program reads a value from databus when Z80 leaves databus control to peripherals. ; ; Usage from MSX-BASIC: ; ; BLOAD "DATABUS.BIN",R:PRINT USR(0) ; ;----------------------------------------------------------------------------------------- USR: EQU #F7F8 USR0: EQU #F39A DEFB #FE DEFW BEGIN DEFW END DEFW START ORG #B000 BEGIN: START: LD HL,CODE LD (USR0),HL RET CODE: DI LD HL,#BF00 LD BC,0 NEXT: LD A,C ADD A,B CP #10 JP NC,NOMORE LD (HL),A INC HL CP #F JP Z,SKIP LD (HL),B INC HL SKIP: INC C LD A,C CP #10 JP NZ,NEXT NOMORE: LD C,0 INC B LD A,B CP #10 JP NZ,NEXT LD (HL),#C0 LD HL,#BF00 LD DE,#BE00 LD BC,0 FIXL: LD A,(HL) LD C,A OR #C0 LD (HL),A INC HL LD A,(HL) RLCA RLCA RLCA RLCA OR C LD E,A EX DE,HL LD A,E DEC A LD (HL),A EX DE,HL DJNZ FIXL LD HL,#C0C0 LD DE,#C0C1 LD B,#10 MAKEPROG: PUSH BC LD BC,#F PUSH HL PUSH DE LD (HL),4 ;INC B LDIR LD (HL),#CD ;CALL INC HL LD DE,CALCUT LD (HL),E INC HL LD (HL),D POP DE POP HL POP BC INC H INC D DJNZ MAKEPROG LD A,#BF LD I,A IM 2 EI NOP HALT ; Any interrupt will be accepted. LD L,A LD H,#BE LD A,(HL) LD (USR),A XOR A LD (USR+1),A RET CALCUT: POP AF AND #F RLCA RLCA RLCA RLCA OR B XOR #F IM 1 EI RETI END:
Maybe something like this would do the trick for you: (Repeats after 256 values)
Rand8 ld a,Seed ; Seed is usually 0
ld b,a
add a,a
add a,a
add a,b
inc a ; another possibility is ADD A,7
ld (Rand8+1),a
rlca
and 1
ret
Ok this looks simple and good enough. It doesn't need to be superrandom. Pseudorandom will do.
So how does this Seed thing work. What about (Rand8+1) why not just use random?
A good addition to NYYRIKKI's code is set the seed using user interaction.
E.g. increasing seed each interrupt at the title screen till the user presses the 'start' key. Or increasing the seed during game play when the user presses no (or a certain) key (cursor,space etc).
The user is the best available random seed generator
Don't you have an account yet? Become an MSX-friend and register an account!
