Random…. More like a scripted Sequence !!!

# Thread: Random…. More like a scripted Sequence !!!

1. ## Random…. More like a scripted Sequence !!!

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…. ???
Last edited by andybarrett1; - 24th October 2014 at 22:48. Reason: Typo

2. ## Re: Random…. More like a scripted Sequence !!!

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

random myword

worth trying ?

ps unconnected = floating
Last edited by richard; - 24th October 2014 at 23:07.

3. ## Re: Random…. More like a scripted Sequence !!!

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.

4. ## Re: Random…. More like a scripted Sequence !!!

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...

5. ## Re: Random…. More like a scripted Sequence !!!

Further to this thread...

Although using :-

random myword

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

random myword1

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:-

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

6. ## Re: Random…. More like a scripted Sequence !!!

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...

7. ## Re: Random…. More like a scripted Sequence !!!

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.

8. ## Re: Random…. More like a scripted Sequence !!!

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

9. ## Re: Random…. More like a scripted Sequence !!!

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.

10. ## Re: Random…. More like a scripted Sequence !!!

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/ap...ex.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.

11. ## Re: Random…. More like a scripted Sequence !!!

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

Thank you all again

Andy

#### Members who have read this thread : 0

You do not have permission to view the list of names.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts