Random seed using the ds1307 RTC


Closed Thread
Results 1 to 8 of 8
  1. #1
    Join Date
    Nov 2007
    Posts
    8

    Question Random seed using the ds1307 RTC

    Hi, first time poster, long time lurker.

    How best can I use my ds1307 RTC to set a unique seed for use with RANDOM.

    I know that the RANDOM seed needs a 4-byte value, and to the best of my knowledge the DS1307 only returns, seconds, minutes, hours, day, month, year, day of week, and not microseconds.

    I want to randomize this as best as I can taking advantage of the RTC.

    Anyone have any advice?

    Thanks

  2. #2
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Can't call it advice, more like an idea.

    When powering up, start a 16-bit timer and let it free run until you get a transition from the 1307's SQW pin.
    Should give a halfways decent seed.

    You'll have to turn on the Square wave output on the 1307 first.
    AND, you need to make sure the seed is never ZERO.
    <br>
    DT

  3. #3
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    Random requires (required) a non-zero WORD variable as a seed... I haven't checked if this has changed with v2.50...

    If that's the case just multiply all the DS1307's CLOCK registers together 'as-is' into a WORD variable without even bothering to convert from BCD. You'll spill out of 16-bits, but it won't matter - PICBASIC just keeps on trucking ignoring the spillage. If the end result is zero then either just add 1, or repeat the itteration.

  4. #4
    Join Date
    Nov 2007
    Posts
    8


    Did you find this post helpful? Yes | No

    Default

    What I want to do is generate a random number between, say, 10 and 30. But from what I did, the numbers progressively get larger in sequence and do not look that random. If the seed is larger, does that mean that the random number generated will be larger? Also, I noticed that the random number generated goes above my max limit.

    Here is my simplified code below with the output following it. How can I improve my code to get a more random looking output? Note that this device won't power cycle that often, so I don't want to rely on the reading/writing to the eeprom on start that much, but I did include it in my code to add an eeprom value to the seed.

    Code:
    RandomSeed Var WORD
    RandVal  VAR Byte
    Cnt2  VAR Byte
    MinCnt  VAR Byte
    MaxCnt VAR Byte
    Cnt2 VAR Byte
    
    MinCnt = 10
    MaxCnt = 30
    
    Read 0, Cnt2
    Write 0, Cnt2 + 7
    
    i2cread SDApin,SCLpin,$D0,$00,[Seconds,Minutes,hours,dow,day,month,year]
    
    Randomseed = (Hours+1) * (Minutes+7) * (Seconds+3) + Cnt2
    Random RandomSeed
    RandVal= (RandomSeed//MaxCnt)+MinCnt
    
    Debug dec2 RandVal,CR,LF

    Output:
    Code:
    10
    10
    12
    12
    14
    14
    28
    28
    30
    33
    33
    35
    35
    36
    36
    38
    38
    11
    11
    13
    13
    15
    15
    17
    17
    31
    31
    33
    33
    34
    34
    36
    39
    39
    Thanks for any help.

  5. #5
    Join Date
    Nov 2007
    Posts
    8


    Did you find this post helpful? Yes | No

    Default

    Can anyone provide some guidance with those 2 issues I'm having? (number generated is out of my range, and number generated always increasing)

  6. #6
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Maybe this might work ...

    A random number is always a word variable.
    The value is always between 1-65535.

    So if the 1-65535 is considered the Decimal part, then multiplying it times the range you want (in your case 20) and take the High Word result, then add the minimum...
    Code:
    Value  VAR WORD
    RND    VAR WORD
    MinCnt CON 10
    MaxCnt CON 30
    Scale  CON MaxCnt - MinCnt 
    
    RND = 1234 ; The Seed, however you get it
    
    RANDOM  RND
    Value = (RND ** Scale) +  MinCnt
    DT

  7. #7
    Join Date
    Oct 2007
    Location
    Vancouver, BC, Canada
    Posts
    33


    Did you find this post helpful? Yes | No

    Default

    Base on Darrel code, this line >>> Scale CON MaxCnt - MinCnt << should be Scale CON MaxCnt - MinCnt +1, so the maximum range will be reached.

    Thanks Darrel.

    I also attached a little file making a set of 6 random numbers range from 10 to 30 based on Darrel code.
    Attached Files Attached Files
    Last edited by KVLV; - 25th November 2007 at 11:31. Reason: commented out some code

  8. #8
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Thumbs up You are Correct!

    Someone's paying attention!

    Yup you're right KVLV.

    + 1
    Why do I always forget that part?

    Thanks for the example too.
    <br>
    DT

Similar Threads

  1. DS1307 RTC example
    By malc-c in forum Code Examples
    Replies: 13
    Last Post: - 12th November 2012, 20:56
  2. 16F628 and an RTC DS1307
    By aurbo in forum General
    Replies: 2
    Last Post: - 7th March 2011, 23:06
  3. PIC 16F877 I2C communications
    By sigmoideng in forum General
    Replies: 7
    Last Post: - 13th July 2007, 11:28
  4. Random number seed
    By kwelna in forum mel PIC BASIC
    Replies: 10
    Last Post: - 1st February 2007, 18:33
  5. X1226 RTC - Help
    By charudatt in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 14th August 2006, 18:54

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