PDA

View Full Version : RANDOM and 12F683 - generates same values each time



CuriousOne
- 24th June 2014, 12:58
Hello.

On power up, I need to get some random numbers.

I'm using the code below:



RANDOM DROEBITI
PIRVELI=(DROEBITI//6)+1
RANDOM DROEBITI
MEORE=(DROEBITI//6)+1
RANDOM DROEBITI
MESAME=(DROEBITI//6)+1
RANDOM DROEBITI
MEOTXE=(DROEBITI//6)+1


I always get same results:

8
7
2
4

Why?

HenrikOlsson
- 24th June 2014, 13:16
Hi,
Because that's how RANDOM works.... From the manual

RANDOM is not a true random-number generator. It performs a complex math operation on the seed value, resulting in "seemingly random" result. The same seed value will always yield exactly the same result.

There are a couple of ways to get a better "randomness". For example, use the ADC and sample a something noisy, use that value as the seed.

ADCIN 1, DROEBITI
RANDOM DROEBITI


/Henrik.

CuriousOne
- 24th June 2014, 15:57
Yes but same code run on 16F870 gives great random numbers?

HenrikOlsson
- 24th June 2014, 16:25
Does it really? You're going to have to convince me on that one ;-)
As far as I understand that's pretty much impossible. Give RANDOM a specific seed and you'll get a specific sequency of "random" number from it. Next time you run it, given the same seed, you'll get the same sequence of numbers.

/Henrik.

CuriousOne
- 24th June 2014, 16:30
I can film a video, if you like :)

HenrikOlsson
- 24th June 2014, 16:35
You could, if you want to, or just post the code (not just the snippet above) for the 16F870 and two or three sequences of the result you're getting.

/Henrik.

CuriousOne
- 24th June 2014, 16:45
include "modedefs.bas"
DEFINE OSC 4
DEFINE LCD_DREG PORTB
DEFINE LCD_DBIT 4
DEFINE LCD_RSREG PORTB
DEFINE LCD_RSBIT 0
DEFINE LCD_EREG PORTB
DEFINE LCD_EBIT 1
DEFINE LCD_BITS 4
DEFINE LCD_LINES 2
DEFINE LCD_COMMANDUS 1500
DEFINE LCD_DATAUS 44
ADCON1=%00000110 'CONFIGURE PORT A AS DIGITAL

PIRVELI VAR BYTE 'CHARTVISAS BEEPEBIS RAODENOBA
MEORE VAR BYTE 'CHARTVISAS LEDEBIS CIMCIMIS RAODENOBA
MESAME VAR BYTE 'meore etapisatvis shemtxveviti ricxvi
MEOTXE VAR BYTE 'MESAME ETAPIS SHEMTXVEVITI RICXVI
DROEBITI VAR WORD 'DROEBITI SHEMTXVEVITIS GENERACIISATVIS
DROEBITI2 VAR WORD
DROEBITI3 VAR WORD
MTVLELI VAR WORD 'VARIABLE FOR COUNTER
bvar1 var byte
bvar2 var byte
ROM1 VAR BYTE
ROM2 VAR BYTE
ROM3 VAR BYTE
ROM4 VAR BYTE

'PRAVI VAR GPIO.0

'DEFINITIONS
TAVKA:
DROEBITI2=0
DROEBITI3=0
DROEBITI=0
READ 1,ROM1
READ 2,ROM2
READ 3,ROM3
READ 4,ROM4



GENERATE: 'CVLADEBIS GENERACIA
RANDOM DROEBITI
PIRVELI=(DROEBITI//6)+1
RANDOM DROEBITI
MEORE=(DROEBITI//6)+1
RANDOM DROEBITI
MESAME=(DROEBITI//6)+1
RANDOM DROEBITI
MEOTXE=(DROEBITI//6)+1

lcdout $fe,$c0,"1=",#pirveli, " 2=",#meore, " "
pause 50
goto generate
end


On each cycle run, it returns different results. With 12F683, results are fixed, does not changing, no matter how many times loop is run.

HenrikOlsson
- 24th June 2014, 17:06
I may be misunderstnading but previously you said that you needed some random numbers on power up. Now you're running the routine in a loop (at least that's how I understand it) - that's not the same thing. Each time thru the loop the seed will be what the last RANDOM statement generated. But when you start from scratch (ie power up) the seed will always be 0. As long as the seed is the same (ie 0 at power up) you'll get the same results - and I'll stick to that for a little while longer ;-)

Run this on a 16F870 and on a 12F683

GENERATE: 'CVLADEBIS GENERACIA
RANDOM DROEBITI
PIRVELI=(DROEBITI//6)+1
RANDOM DROEBITI
MEORE=(DROEBITI//6)+1
RANDOM DROEBITI
MESAME=(DROEBITI//6)+1
RANDOM DROEBITI
MEOTXE=(DROEBITI//6)+1

lcdout $fe,$c0,"1=",#pirveli, " 2=",#meore, " "
pause 50
end
Cycle the power and run it again. Does it really give you A) Random numbers on the 16F870 but not on the 12F683 and/or B) Different results on 16F870 compared to the 12F683?

/Henrik.

CuriousOne
- 24th June 2014, 17:15
Tested the above code. On power up, it gives 3 and 6 on 16f870 and 2 and 4 on 12f683, same results on each power up.

however, if I place it in the loop, results on 16f870 are changing, while on 12f683 they remain 2 and 4.

HenrikOlsson
- 25th June 2014, 06:31
Hi,
OK, the fact that you don't get the exact same sequence from the two devices could possibly (I don't know) come from the different instruction width (12 vs 14 bits) between the two. But why the 12F683 doesn't continue the sequence when the routine is looped is beyond me.

/Henrik.

wdmagic
- 1st July 2014, 22:49
I have found that just about all random number generators on pics, and PCs generate the same it has to do with seed values.
my solution was to have it generate random numbers repeatedly very fast (on a timer) and have the program grab numberswhen needed. instead of calling for a random number, your calling for whatever number is currently being generated.

peterdeco1
- 2nd July 2014, 14:54
A while back I had issues generating random numbers. Each time a pushbutton was pushed I needed a random number which very often repeated the same sequence. I did the following. It was also checking a port for a button push while generating numbers and did a specific task depending on the number.

START:
LET NUMBER = (NUMBER + 1)
IF NUMBER >=250 Then LET NUMBER = 0
GOTO START

ruijc
- 2nd July 2014, 16:55
If you have spare pins, use one ADC pin to capture nothing but noise and that will give you your random number.

Always different for sure :D