Example of random number generator (Development Foros MSX)MSX Resource Center            
                       
English Nederlands Espa�ol Portugu�s Russian                  
 Noticias
   Página principal
  Almacén de noticias
  Temas de noticias

 Recursos
   Foros MSX
  Artículos
  Analisis
  Informe de ferias/RUs
  Álbum de fotos
  Ferias y encuentros
  Encuestas
  Enlaces
  Buscar

 Software
   Descargas
  Tienda Online

 MRC
   Quiénes somos
  Únete a nuestro equipo
  Donar
  Políticas
  Contacta con nosotros
  Enlázanos
  Estadísticas

 Buscar
 
  

  

 Login
 

Login

Contraseña




¿Aún no tienes una cuenta? ¡Conviértete en miembro del MSX Resource Center! ¡Únete a nosotros!.


 Estadísticas
 

Hay 47 invitados y 5 miembros en línea

Eres un usuario anónimo.
 

Foros MSX


Foros MSX

Development - Example of random number generator

Ir a la página ( Página anterior 1 | 2 )
Autor

Example of random number generator

ARTRAG
msx master
Mensajes: 1802
Publicado: Marzo 18 2008, 15:57   
A faster uniform generator for 7 bit numbers should be :
rand:
                ld   a,r
                ld   b,a
                ld   a,(old_rnd)
                xor  b
                ld   (old_rnd),a
                ret


IMHO seems even more uniform than the first one posted at the beginning of this tread...
Anyone willing confirm my impression?


NYYRIKKI
msx master
Mensajes: 1533
Publicado: Marzo 18 2008, 16:18   

pitpan
msx master
Mensajes: 1418
Publicado: Marzo 18 2008, 16:45   
Sooooooooooo good!

Anyway, ARTRAG, you should remember to init the R register with a 0 to have numbers between 0 and 127. Something like XOR A ; LD R,A
ARTRAG
msx master
Mensajes: 1802
Publicado: Marzo 18 2008, 17:59   
Well, it generates 7 bit random numbers even if bit 7 stays to 1.

Any idea on how good it is ?
MOA
msx freak
Mensajes: 152
Publicado: Marzo 20 2008, 01:10   
Quote:

Well, it generates 7 bit random numbers even if bit 7 stays to 1.

Any idea on how good it is ?



I just incorporated it in my z80 asm random maze generator with mixed results... the mazes look ok (although patterns are visible), but the loops that distribute treasure [making sure there's only 1 treasure per Y tile to avoid too many sprites per row] sometimes gets stuck in an endless (?) loop, waiting for a suitable random number.

Using the high byte (ld A,H) of this function gives very nice results:

SEED: equ rand16 + 1

rand16:
ld BC,0
ld HL,253
xor A
sbc HL,BC
sbc A,B
sbc HL,BC
sbc A,B
ld C,A
sbc HL,BC
jr nc,.end
inc HL
.end
ld (SEED),HL ; self modifying code (seed is hardcoded in opcode)
ret

p/s: this function was ripped of the internet and slightly optimized by me.

poke-1,170
msx professional
Mensajes: 908
Publicado: Marzo 20 2008, 03:10   
poke-1,170
msx professional
Mensajes: 908
Publicado: Marzo 20 2008, 03:10   

poke-1,170
msx professional
Mensajes: 908
Publicado: Marzo 20 2008, 03:10   
speaking of random numbers....

http://youtube.com/watch?v=qjOZtWZ56lc
ro
msx guru
Mensajes: 2353
Publicado: Marzo 20 2008, 08:37   
I'd like a number 2, please....
ARTRAG
msx master
Mensajes: 1802
Publicado: Marzo 20 2008, 10:57   
Quote:

Quote:

Well, it generates 7 bit random numbers even if bit 7 stays to 1.

Any idea on how good it is ?



I just incorporated it in my z80 asm random maze generator with mixed results... the mazes look ok (although patterns are visible), but the loops that distribute treasure [making sure there's only 1 treasure per Y tile to avoid too many sprites per row] sometimes gets stuck in an endless (?) loop, waiting for a suitable random number.

Using the high byte (ld A,H) of this function gives very nice results:

SEED: equ rand16 + 1

rand16:
   ld	BC,0
   ld	HL,253
   xor A
   sbc HL,BC
   sbc A,B
   sbc HL,BC
   sbc A,B
   ld	C,A
   sbc HL,BC
   jr	nc,.end
   inc HL
.end
   ld	(SEED),HL ; self modifying code (seed is hardcoded in opcode)
   ret

p/s: this function was ripped of the internet and slightly optimized by me.



Thanks!! This is a true random generator.
Actually my code can fall in "loops" (i.e. bad random sequences) when it is called in a loop itself without branches.
This is due to the fact that R counts the number of executed instructions, so when the loop calling the rand generator
has no branches the counter is increased of a fixed quantity and bad sequences can happen.

I call my rand generator after some large switches so in my case I do not incur in big problems.
Anyway I see it is not very random.
Fudeba
msx lover
Mensajes: 74
Publicado: Marzo 20 2008, 15:28   
Quote:

A faster uniform generator for 7 bit numbers should be :
rand:
                ld   a,r
...

IMHO seems even more uniform than the first one posted at the beginning of this tread...
Anyone willing confirm my impression?



I believe using R register is a nice thing to get a single random value, given that at least one user interaction was required since R register was reset. You can get random (pseudo, of course) sequences using it several times, but I believe they probably will not be uniform.
For uniform random sequences it is better to use a LFSR function, as the original posted one (very well explained by RicBit and the links he provided). It seems weird, but it really works.

ARTRAG
msx master
Mensajes: 1802
Publicado: Marzo 20 2008, 16:00   
I'm sure of that. But this is shorter and seems fine for my needs.

rand:       
                ld   a,r
old_rnd:   
                xor  0
                ld   (old_rnd+1),a
                ret


For the 2K competition size wins on efficiency

 
Ir a la página ( Página anterior 1 | 2 )
 







(c) 1994 - 2009 Fundación MSX Resource Center. MSX es una marca registrada de MSX Licensing Corporation