RANDOM and 12F683 - generates same values each time


Closed Thread
Results 1 to 13 of 13

Hybrid View

  1. #1
    Join Date
    Feb 2013
    Posts
    1,122

    Default RANDOM and 12F683 - generates same values each time

    Hello.

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

    I'm using the code below:

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

  2. #2
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,604


    Did you find this post helpful? Yes | No

    Default Re: RANDOM and 12F683 - generates same values each time

    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.
    Code:
    ADCIN 1, DROEBITI
    RANDOM DROEBITI
    /Henrik.

  3. #3
    Join Date
    Feb 2013
    Posts
    1,122


    Did you find this post helpful? Yes | No

    Default Re: RANDOM and 12F683 - generates same values each time

    Yes but same code run on 16F870 gives great random numbers?

  4. #4
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,604


    Did you find this post helpful? Yes | No

    Default Re: RANDOM and 12F683 - generates same values each time

    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.

  5. #5
    Join Date
    Feb 2013
    Posts
    1,122


    Did you find this post helpful? Yes | No

    Default Re: RANDOM and 12F683 - generates same values each time

    I can film a video, if you like

  6. #6
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,604


    Did you find this post helpful? Yes | No

    Default Re: RANDOM and 12F683 - generates same values each time

    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.

  7. #7
    Join Date
    Dec 2012
    Location
    Tennessee
    Posts
    262


    Did you find this post helpful? Yes | No

    Default Re: RANDOM and 12F683 - generates same values each time

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


    Any man who has accomplished anything in electronics at one time or another has said... " STOP! WAIT! NOOO! Dangit.... Oh Well, Time to start over..."

  8. #8


    Did you find this post helpful? Yes | No

    Default Re: RANDOM and 12F683 - generates same values each time

    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

  9. #9


    Did you find this post helpful? Yes | No

    Default Re: RANDOM and 12F683 - generates same values each time

    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

Similar Threads

  1. Adding values one at a time and sending them serially
    By Megahertz in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 21st August 2013, 14:56
  2. Why does this PBP statement post the wrong time values?
    By jellis00 in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 22nd November 2011, 03:10
  3. DS1904 RTC - How to Convert Binary Time into Real Time/Date?
    By Ioannis in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 2nd December 2010, 10:45
  4. LCDOUT generates errors.
    By PICante in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 17th September 2009, 17:58
  5. Linx RF -> HSERIN time delay / time critical app...
    By batee in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 15th October 2004, 15:04

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