Random…. More like a scripted Sequence !!!


Closed Thread
Results 1 to 11 of 11
  1. #1
    Join Date
    May 2012
    Location
    Merseyside, UK
    Posts
    237

    Default 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. #2
    Join Date
    May 2013
    Location
    australia
    Posts
    2,389


    Did you find this post helpful? Yes | No

    Default 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

    adcin ch,myword
    random myword

    worth trying ?

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

  3. #3
    Join Date
    Apr 2011
    Location
    Welches, Oregon
    Posts
    198


    Did you find this post helpful? Yes | No

    Default 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. #4
    Join Date
    May 2012
    Location
    Merseyside, UK
    Posts
    237


    Did you find this post helpful? Yes | No

    Cool 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. #5
    Join Date
    May 2012
    Location
    Merseyside, UK
    Posts
    237


    Did you find this post helpful? Yes | No

    Unhappy Re: Random…. More like a scripted Sequence !!!

    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

  6. #6
    Join Date
    Sep 2009
    Posts
    737


    Did you find this post helpful? Yes | No

    Default 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. #7
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,521


    Did you find this post helpful? Yes | No

    Default 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. #8
    Join Date
    May 2012
    Location
    Merseyside, UK
    Posts
    237


    Did you find this post helpful? Yes | No

    Default 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. #9
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,521


    Did you find this post helpful? Yes | No

    Default 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. #10
    Join Date
    Sep 2008
    Location
    Maine, USA
    Posts
    81


    Did you find this post helpful? Yes | No

    Default 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.
    "Do or do not, there is no try" Yoda

  11. #11
    Join Date
    May 2012
    Location
    Merseyside, UK
    Posts
    237


    Did you find this post helpful? Yes | No

    Default Re: Random…. More like a scripted Sequence !!!

    Interesting read Tekart..

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

    Thank you all again

    Andy

Similar Threads

  1. Replies: 13
    Last Post: - 5th September 2010, 01:55
  2. How to set sequence of interrupt for PIC
    By chai98a in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 21st June 2008, 11:17
  3. FOR sequence is skipped
    By selbstdual in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 10th August 2006, 02:35
  4. Installation sequence
    By Demon in forum General
    Replies: 23
    Last Post: - 11th July 2006, 03:56
  5. Switch Sequence
    By Tissy in forum mel PIC BASIC Pro
    Replies: 11
    Last Post: - 13th February 2005, 20:36

Members who have read this thread : 1

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

Tags for this Thread

Posting Permissions

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