RANDOM and 12F683 - generates same values each time


Closed Thread
Results 1 to 13 of 13
  1. #1
    Join Date
    Feb 2013
    Posts
    1,078

    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,520


    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,078


    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,520


    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,078


    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,520


    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
    Feb 2013
    Posts
    1,078


    Did you find this post helpful? Yes | No

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

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

  8. #8
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,520


    Did you find this post helpful? Yes | No

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

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

  9. #9
    Join Date
    Feb 2013
    Posts
    1,078


    Did you find this post helpful? Yes | No

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

    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.

  10. #10
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,520


    Did you find this post helpful? Yes | No

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

    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.

  11. #11
    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..."

  12. #12


    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

  13. #13


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

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