S08 Random Number Generator

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

S08 Random Number Generator

9,038 Views
Davo
Contributor II
I am looking for a way of generatring an 8 bit random number using an S08. Preferably in assembly.  I have searched the web endlessly but haven't found anything simple and usable.  Any ideas appreciated.
 
Labels (1)
23 Replies

419 Views
Roach
Contributor I
If you have an AD input or time input, you may use them to generate your random number.
0 Kudos
Reply

419 Views
bigmac
Specialist III
Hello,
 
Use of the Lehmer algorithm, as suggested by JimD, may be somewhat overkill for an 8-bit result, since 32-bit quantities are used during the calculation.
 
You might also consider a pseudo-random sequence (PRS), similar to that used for CRC calculations.  The suitability will depend on your application.  For an 8-bit PRS, the pattern will repeat after 255 iterations, and for 16-bit, the sequence length would be 65535 iterations. Each of the possible (non-zero) values will occur once within each sequence.
 
The starting point for the sequence will be determined by initialisation of a "seed" value.  If the starting point should be different for each equipment, the seed must be related to an external event.  Perhaps read the TPM counter when this randomly timed event occurs, and use the LS byte as the seed.
 
The following 8-bit PRS is based on a CRC function coded in C, and then converted to ASM.  The ASM code is untested.  The 8-bit polynomial used is X^8 + X^5 + X^4 + 1.  The subroutine assumes that you have allocate a byte variable RAND, preferably within zero page RAM, and that this has been initialised to the required non-zero seed value.
 
POLYVAL EQU $8C  ; Polynomial value
 
; PRS sub-routine:
; On exit, ACC = next value of sequence
 
NEXTVAL:
      LDX   #8
      LDA   RAND
NV1:  LSRA
      BCC   NV2
      EOR   #POLYVAL
NV2:  DBNZX NV1  ; Loop for next bit
      STA   RAND
      RTS  
 
This process could be easily extended to a 16 bit PRS, obviously using a suitable 16-bit polynomial.
 
Regards,
Mac
 
0 Kudos
Reply

419 Views
JimDon
Senior Contributor III
The first question is how "random" does it need to be?
What are you going to use it for?
"Good" random generators are just not as simple as you may like.

This is about as simple as it gets.

Read this to see if it it will do.
It this is good enough, then I am sure the asm guys will jump in to tell you the best way to do it.


Code:
unsigned long int lehmer(long int s)//  linear congruential pseudo based on D. Lehmer{  static unsigned long long a = 2007, b = 4194301, c = 2147483647, z = b;    if ( s < 0 ) {s = -s; a = s;}    z = (a + b * z) % c;    return z % s;}

 



Message Edited by JimDon on 2008-09-16 09:40 AM
0 Kudos
Reply