www.retrosoftware.co.uk
http://www.retrosoftware.co.uk/forum/

Random number generator (very fast - very basic)
http://www.retrosoftware.co.uk/forum/viewtopic.php?f=73&t=640
Page 1 of 1

Author:  tricky [ Wed Jun 29, 2011 10:18 am ]
Post subject:  Random number generator (very fast - very basic)

This is about the fastest random number generator you can get although it is really just a sequence of 256 numbers using 14 bytes (code and data if seed is in ZP)

An .ssd is attached and you can see the description here http://www.retrosoftware.co.uk/wiki/index.php/Random_number_generator_%28very_fast_-_very_basic%29

Code:
L.
   10*K.10O.|MREN.|M*FX12 4|ML.|M
   20FOR O = 0 TO 2 STEP 2
   30seed = &70
   80P%=&5200
   90[
  100OPT O
  110.rand
  120LDA seed
  130ASL A
  140ASL A
  150CLC
  160ADC seed
  170CLC
  180ADC #&45
  190STA seed
  200RTS
  210]
  220NEXT
  230FOR N = 0 TO 255
  240CALL rand
  250PRINT " ";~?seed;
  260NEXT


Attachments:
File comment: rand BBC test program .ssd
rand.zip [686 Bytes]
Downloaded 14 times

Author:  RichTW [ Wed Jun 29, 2011 4:42 pm ]
Post subject:  Re: Random number generator (very fast - very basic)

That's actually a pretty good algorithm. If you weren't worried about reproducibility, you could EOR with a timer at the end, e.g. EOR &FE44, which should add a bit more randomness!

Author:  tricky [ Wed Jun 29, 2011 4:56 pm ]
Post subject:  Re: Random number generator (very fast - very basic)

I was tempted to self mod the addition every time the seed gets to 0 to chain many different sequences but the timer is definitely in the spirit of this code.

Author:  tricky [ Thu Jun 30, 2011 1:07 pm ]
Post subject:  Re: Random number generator (very fast - very basic)

As a little side note to newer programmers, it is very useful to keep separate random numbers for game/sound/graphics/etc (especially in multi-machine synchronised state games) to aid in debugging and at the cost of a few bytes, well worth it.

When debugging, I would recommend not using Rich's excellent tip (of EORing with the timer low-byte) or NOPping it out in a binary to make bug reproduction easier.

Page 1 of 1 All times are UTC [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/