Autor
| random generator 1 to 3
| mighty.m msx user Mensajes: 64 | Publicado: Octubre 12 2003, 20:59   | Anyone of you have a good idea on how to create a random number from 1 to 3 in ASM?
<TABLE BORDER=0 ALIGN=CENTER WIDTH=85%><TR><TD><font class="mrc-small">Code:</font><HR></TD></TR><TR><TD><FONT class="mrc-small"><PRE>
random:
ld a,r
ld b,8
loop:
rra
rl c
djnz loop
ld hl,0
add hl,bc
add hl,bc
add hl,bc
ld a,h
inc a
ret
</PRE></FONT></TD></TR><TR><TD><HR></TD></TR></TABLE>
i already have something like this but it doesn't quite do the trick...
any help is welcome
| | msd msx professional Mensajes: 618 | Publicado: Octubre 12 2003, 21:21   | You can use the refresh register to get a "random" number
| | BiFi msx guru Mensajes: 3142 | Publicado: Octubre 12 2003, 21:37   | the source states a LD A,R already
a simple and tiny one is this:
loop: ld a,r
and 3
jr z,loop
| | msd msx professional Mensajes: 618 | Publicado: Octubre 12 2003, 22:34   | Not all bits are used in the r register
Don't now if it is the lowest or the highest bit that is unused
| | ro msx guru Mensajes: 2347 | Publicado: Octubre 12 2003, 22:39   | r u sure about that MSD? can't remember having that problem
from 1 to 3 eh..
<TABLE BORDER=0 ALIGN=CENTER WIDTH=85%><TR><TD><font class="mrc-small">Code:</font><HR></TD></TR><TR><TD><FONT class="mrc-small"><PRE>
loop:
ld a,r
and 3
jr z,loop
ret
</PRE></FONT></TD></TR><TR><TD><HR></TD></TR></TABLE>
will generat 0, 1,2 or 3. if it's zero, it'll try again.
| | ro msx guru Mensajes: 2347 | Publicado: Octubre 12 2003, 22:48   | what and why wanna use it for might m?
| | GuyveR800 msx guru Mensajes: 3048 | Publicado: Octubre 13 2003, 01:28   | bit 7 of the R register remains untouched.
If you store 0 in R, it will count from 0 to 127. If you store 128 in R, it will count from 128 to 255.
In other words, you can store a whole extra bit in the R register! Might be cool for optimizing storage of carry-flag or something  | | GuyveR800 msx guru Mensajes: 3048 | Publicado: Octubre 13 2003, 01:58   | Other R trivia:
On R800 the R register is simulated, it has no connection with the memory refresh anymore.
(So loop: XOR A : LD R,A : JP LOOP doesn't 'damage' the memory)
On Z380 the R register is untouched and can be used as an extra 8-bit register!
| | kanima msx lover Mensajes: 80 | Publicado: Octubre 13 2003, 02:08   | Quote:
| the source states a LD A,R already
a simple and tiny one is this:
loop: ld a,r
and 3
jr z,loop
|
Since the R register isn't a random one, but rather one that is just incremented all the time (see below) there's a small problem with the above code: when the result of the and 3 is zero then the result of the next iteration of the loop will always result in the same value. I think it's 3, but I might have miscounted something. This would mean that the value 3 has a 50% chance of occuring and 1 and 2 have a 25% each.
Things can get even worse of course if this random-routine is used inside a loop. Let's for example assume this random-routine is being used to fill a piece of memory:
ld hl,#4000
ld bc,#100
fillloop:
randomloop:
ld a,r
and 3
jr z,randomloop
ld (hl),a
inc hl
dec bc
ld a,b
or c
jp nz, fillloop
As you can see, all the other code in the loop is non-conditional which means it'll always execute in the same number of cycles and R will be increased by the same value.
So, every time the and 3 gives a 1, 2 or 3 you can calculate what the result of the and 3 in the next iteration will be and for 1 it will always be the same as it is for 2 and for 3 (and as 0 -> 3 also for 0).
This means you either get the same value every time:
1,1,1,1,1,1,1 or
2,2,2,2,2,2,2 or
3,3,3,3,3,3,3
or you get a repetetive series:
...1,2,3,1,2,3,1,2,3,1,2,3... or
...1,3,2,1,3,2,1,3,2...
Neither of which is desirable.
To quote from "The undocumented Z80 documented"
Quote:
|
The R register is increased at every first machine cycle (M1). Bit 7 of the
register is never changed by this; only the lower 7 bits are included in the
addition. So bit 7 stays the same, but it can be changed using the LD R,A
instruction.
Instructions without a prefix increase R by one. Instructions with an ED,
CB, DD, FD prefix, increase R by two, and so do the DDCBxxxx and FDCBxxxx
instructions (weird enough). Just a stray DD or FD increases the R
by one. LD A,R and LD R,A access the R register after it is increased (by the
instruction itself).
|
| | ro msx guru Mensajes: 2347 | Publicado: Octubre 13 2003, 12:09   | hmm, funny. didn't know that (anymore?!)
| | kanima msx lover Mensajes: 80 | Publicado: Octubre 13 2003, 13:34   | Quote:
|
i already have something like this but it doesn't quite do the trick...
any help is welcome
|
Take a look at members.lycos.co.uk/leeedavison/z80/prng/
This will give you an 8-bit random value in A (and if you also take the carry into account you could view it as a 9-bit random value) from which you then get to a value of 1-3....
| | mighty.m msx user Mensajes: 64 | Publicado: Octubre 13 2003, 20:35   | sorry guys for the late reply, but I can only connect to the internet at home... so here it goes:
I'm working on a little project (can't tell more) but I need a random number from 1 to 3...
I already tried the short sourcy (ld a,r + and with mask 3)... It just isn't random enough... some number pops up more often than the others...
| | pitpan msx master Mensajes: 1390 | Publicado: Octubre 13 2003, 21:13   | Use a bigger table with 128 entries and do something like the following:
xor a
ld r,a (to mantain R between 0 and 127)
....
ld a,r
ld b,0
ld c,a
ld hl,TABLE
add hl,de
ld a,[hl]
....
TABLE: db 0,1,2,3,1,2,3,0,2,3,0,1,3,0,1,2,... Really random, please!
I know this is really unoptimized, but I am a Gameboy freak, I do not use IX or IY. And I prefer 16-bit arithmetic.
Kind regards,
Ed Robsy
| | NYYRIKKI msx master Mensajes: 1528 | Publicado: Octubre 13 2003, 21:16   | Google helps...
<TABLE BORDER=0 ALIGN=CENTER WIDTH=85%><TR><TD><font class="mrc-small">Code:</font><HR></TD></TR><TR><TD><FONT class="mrc-small"><PRE>
Rand: ld de,0
seed: equ $-2
ld a,d
ld h,e
ld l,253
or a
sbc hl,de
sbc a,0
sbc hl,de
ld d,0
sbc a,d
ld e,a
sbc hl,de
jr nc,Rndjmp
inc hl
Rndjmp: ld (seed),hl
ld a,l
and 3
jr z,Rand
ret
</PRE></FONT></TD></TR><TR><TD><HR></TD></TR></TABLE>
| | sjoerd msx addict Mensajes: 450 | Publicado: Octubre 14 2003, 22:55   | A variation of the one I use in Realfun to randomize a FM voice:
random13:
ld a,r
ld c,a
ld a,(bc)
ld hl,seed
add a,(hl)
add a,c
ld (hl),a
and 3
jr z,random13
ret
seed:
byte 0
| |
| |
| |