random generator 1 to 3 (Development MSX Fora)MSX Resource Center PassionMSX MSX2 contest           
            
English Nederlands Español Português Russian         
 Nieuws
   Voorpagina
  Nieuws archief
  Nieuws onderwerpen

 Informatie
   MSX Fora
  Artikelen
  Recensies
  Beursverslagen
  Fotoreportages
  Beurzen en meetings
  Enquêtes
  Links
  Zoek

 Software
   Downloads
  Webshop

 MRC
   Wie we zijn
  Kom bij ons team
  Doneren
  Policies
  Contact met het MRC
  Link naar Ons
  Statistieken

 Zoek
 
  

  

 Login
 

Gebruikersnaam

Wachtwoord




Ben je nog niet lid? Klik hier en word MSX vriend!


 Statistieken
 

Er zijn 123 gasten en 1 MSX vriend online

Je bent een anonieme bezoeker.
 

MSX Fora


MSX Fora

Development - random generator 1 to 3

Schrijver

random generator 1 to 3

mighty.m
msx user
Berichten: 61
Geplaatst: 12 Oktober 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
Berichten: 607
Geplaatst: 12 Oktober 2003, 21:21   
You can use the refresh register to get a "random" number
BiFi
msx guru
Berichten: 3142
Geplaatst: 12 Oktober 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
Berichten: 607
Geplaatst: 12 Oktober 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
Berichten: 2307
Geplaatst: 12 Oktober 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
Berichten: 2307
Geplaatst: 12 Oktober 2003, 22:48   
what and why wanna use it for might m?
GuyveR800
msx guru
Berichten: 3048
Geplaatst: 13 Oktober 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
Berichten: 3048
Geplaatst: 13 Oktober 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
Berichten: 80
Geplaatst: 13 Oktober 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
Berichten: 2307
Geplaatst: 13 Oktober 2003, 12:09   
hmm, funny. didn't know that (anymore?!)
kanima
msx lover
Berichten: 80
Geplaatst: 13 Oktober 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
Berichten: 61
Geplaatst: 13 Oktober 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
Berichten: 1367
Geplaatst: 13 Oktober 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
Berichten: 1500
Geplaatst: 13 Oktober 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
Berichten: 443
Geplaatst: 14 Oktober 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
 
 







(c) 1994 - 2008 Stichting MSX Resource Center. MSX is een trademark van MSX Licensing Corporation.