Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

8 Bit Random Number Generator

Status
Not open for further replies.

Suraj143

Active Member
I need a good 8 bit random number generator.I have searched in pic list & found this one.

I have done many experiments using this code but still I'm not satisfied.Because this code will generates some similar numbers most of the time.If you test this using a light bar you will notice that.

Is there anyway to fine tune this to make some good random numbers :)

Code:
		RLF     RANDOM,W
                RLF     RANDOM,W
                BTFSC   RANDOM,4
                XORLW   1
                BTFSC   RANDOM,5
                XORLW   1
                BTFSC   RANDOM,3
                XORLW   1
                MOVWF   RANDOM
                RETLW   0
 
Last edited:
Hi, Suraj

This one ( from microchip AN 544 ) is said to be one of the bests ...

Code:
;                       Random Number Generator
;
; This routine generates a 16 Bit Pseudo Sequence Random Generator
; It is based on Linear shift register feedback. The sequence
; is generated by (Q15 xorwf Q14 xorwf Q12 xorwf Q3 )
;
;    The 16 bit random number is in location RandHi(high byte)
;  & RandLo (low byte)
;
;       Before calling this routine, make sure the initial values
; of RandHi & RandLo are NOT ZERO
;       A good chiose of initial random number is 0x3045
;*******************************************************************
;
Random16
	rlcf     RandHi,W
	xorwf     RandHi,W
	rlcf     WREG, F            ; carry bit = xorwf(Q15,14)
;
	swapf    RandHi, F
	swapf    RandLo,W
	rlncf      WREG, F
	xorwf     RandHi,W        ; LSB = xorwf(Q12,Q3)
	swapf    RandHi, F
	andlw   0x01
	rlcf     RandLo, F
	xorwf     RandLo, F
	rlcf     RandHi, F
	return
;

BUT it's false believing getting twice the same number is not random ...

Random can be everyting including same !!! ... routine is " not so good " , only if numbers sequences reappear frequently ...

Alain
 
Last edited:
There is another random number generator on Piclist, that I can't seem to find right now. It used a real simple 'Anding function that may give a more visually appealing result and has an even distribution. Hope this is right, n is the number of bits used.

Code:
 [SIZE="4"][COLOR="Red"]BAD, SEE LINK[/COLOR][/SIZE]     [I]Random = (Random + 2exp(π-1)) AND 2exp(n-1)[/I]

Edit: Ignore above code it was wrong, found the Piclist post link
 
Last edited:
If you're using the shift/XOR method like the OP's code, I would make the shift register 16-32 bits wide instead of 8 bits, and then just grab 8 bits of it to get your random numbers. The more bits the less cyclical/repeating the sequence will be. You can always experiment with which bits you poll to do the XOR (the btfsc instructions).
 
Hi Guys thanks for your useful information.

Hi Acetronics.

That's a very nice routine.I'll use that.

@ kpatz

Your information is really important to me.That's what I going to do.If I need a 8 bit number then I must use a 16bit so it will make less repeating numbers.

The one I tested generates numbers like this
b'11111110,11100111,10011111,

You can see there are many 1s than zeros.What I expect numbers like this with less 1s.
10000010,10010000,00010100
 
Hi Acetronics.

Let say I assigned 1 to the 16 bit number.
ex:00000000 00000001 (.1)

Now I call "Random16" subroutine.So it will generate a new random number.Now again I want a new random number so do I have to move 00000000 00000010 (.2) to the 16 bit number Or just call the "Random16" subroutine?

Because I need 48 random numbers.
ex:random,delay,random,delay................48 random.
 
Last edited:
If you need a slightly better random number generator, try,
Code:
Rand		movlw	8
		movwf	Count
RandLoop	movfw	RndHi
		andlw	b'10110100'	;keep bits 15,13,12 and 10
		addlw	b'01100000'	;do 15 xor 13
		andlw	b'10010100'	;get rid of unwanted bits
		addlw	b'00001100'	;do 12 xor 10
		andlw	b'10010000'	;keep results in 15 and 12
		addlw	b'01110000'	;do 15 xor 12
		addlw	b'10000000'	;move result to carry
		rlf	RndLo,f
		rlf	RndHi,f
		decfsz	Count,f
		goto	RandLoop
		movfw	RndLo
		return
it xors together bits 15,13,12 and 10 to get a new random bit. It does this 8 times so a new random byte is generated.

The sequence won't repeat until 65535 numbers have been generated.

Note, RndLo and RndHi should be seeded with none zero values at the start.

Edit, for an explanation of how it works see Linear feedback shift register - Wikipedia, the free encyclopedia

Mike.
 
Last edited:
Hi Mike thanks for your code.That is more suited where the random numbers need rarely.But I need to call 48 times the "call Random" subroutine so it will loose many cycles.Because each subroutine has 8 times loop back.
 
It only takes 100 cycles per call so for 48 numbers it will take 4.8mS.

Mike.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top