PDA

View Full Version : Random…. More like a scripted Sequence !!!



andybarrett1
- 24th October 2014, 22:47
Hi all thank you for reading..


MyWord var Word
MyByte var Byte

Random MyWord
MyByte=(MyWord//6)+1

Using the above basis I have created what I thought to be a "Dice" type program to give me a "random" number between 1 and 6… however I have just found the resulting output is scripted (Not Random). Has anyone else discovered this random bug, or is it just me…. ???

richard
- 24th October 2014, 23:03
from the manual

RANDOM is not a true random-number generator. It performs a complex math
operation on the seed value, resulting in a "seemingly random" result. The same
seed value will always yield exactly the same result. If the result is used for the
seed value in subsequent iterations of RANDOM, the result is a predictable
repeating sequence of numbers.

what some do is seed the random with a adcin reading on a unconnected analog pin

eg

adcin ch,myword
random myword

worth trying ?

ps unconnected = floating

Amoque
- 24th October 2014, 23:05
It is not a bug. Computer systems of all types are, at their core, logic devices - incapable of random or chaotic operation. More complex systems simulate randomness by "seeding" their random generator with, perhaps, the bit-sum of some area of uninitialized memory or an algorithm based on the time and date. In this way they start the "random" sequence at an unknowable (but calculated) location - this gives the appearance of randomness, but is not.

On such a simple system as the PIC, this "seeding" is left to the designer. You might, for example, loop from 0 to the current value of Timer1 before selecting your "random" value - unlikely "rolls" will be so perfectly timed as to make the outcome calculable.

andybarrett1
- 25th October 2014, 13:37
Hi all the trick of using the ADC port worked great..... Well for what I need anyway. Added to my little books of tricks ..

Only issue was the 628 I was using didnt have ADC so migrated to a 818.

Thank you again...

andybarrett1
- 27th October 2014, 08:43
Further to this thread...

Although using :-

adcin ch,myword
random myword

I am using the same ADC port to create 2 random numbers....:-

adcin 4,myword1
random myword1

adcin 4,myword2
random myword2

Altough the sequence is random..... I am getting repeats (the two myword results are the same)....!

Is it doable to multiply/ divide/ add the myword by a number to change iton one of the words:-

adcin 4,myword1
myword1=myword1*7 ' (myword2=myword2*5 for second one)
random myword1

I have tried the above and differing variables of it ..... But I still get the repeating outputs. Channel 4 is the only ADC channel spare enough to be doing nothing.

Thank you again for reading. Hope it makes sense

Andy

pedja089
- 27th October 2014, 09:49
If you have some user input on pic, you can use that for seed value by measuring time between button press and release. But again RANDOM just use some math and probably after some time repeat result.
For dice I used this code:
Main:
For i = 1 to 6
If button=pressed Then GoSub ShowResult
Next i
GOTO Main
That is closest to random as you can get it. Result depending only on user button press. Variable will change value about 200 000 times in one second, so there is no way to repeat sequence...

HenrikOlsson
- 27th October 2014, 10:09
Hi,
If the ADC aproach isn't working then using one of the PICs timers in freerunning mode to provide the seed might do it. For this to work the execution of time between each instance of the random instruction must not be constant.

/Henrik.

andybarrett1
- 29th October 2014, 08:56
Hi all.

Thanks for help / ideas.... been playing with a couple. Seems to be a lot of repeat numbers... Maybe that just me looking to hard into it.

I really thought multiplying the seed number by two different prime numbers would help..... ! It must mustn't it ?

BR
Andy

HenrikOlsson
- 29th October 2014, 10:51
Hi Andy,
If you give it a certain seed value you'll get a certain "random" sequence of number and that sequency will be the same every time you give it that particular seed value. Mutliplying any seed value by a constant number (prime or not) will only result in a sequence of number like if you've given it that particular seed number from the beginning - it won't make it more random. It may even be so (easy to check) that the seed value is actually only a pointer into the sequence of numbers, ie where to start in the static sequence of "random" numbers.

For something like a dice whice involves human interactin you have the best "random" generator available in the form of time. Let a timer free run and just grab its value when a new number is needed. There's no way a user is going to be able to "pick" a certain number.

/Henrik.

tekart
- 30th October 2014, 23:58
Almost all CPUs (including PICs I think) use a simple logic circuit known as a "pseudo random number generator". It is basically a shift register that feeds back on itself producing a finite series of random numbers. In the pic I believe it is around 36K words long, but I may be wrong. Here is a good article about this:
http://www.maximintegrated.com/en/app-notes/index.mvp/id/1743

One of my tricks for generating random numbers is to read ambient light using a photocell and a resistor to create a divider. This feeds into any A/D input with as much resolution as you need.

I have also successfully used pedja089's solution of timing intervals between button presses if they are frequent enough.

andybarrett1
- 31st October 2014, 11:49
Interesting read Tekart..

Will have a play with the LDR (PhotoDiode) on the ADC.... Sounds like it might work !!

Thank you all again

Andy