PDA

View Full Version : Random seed using the ds1307 RTC



rookie11
- 22nd November 2007, 19:53
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

Darrel Taylor
- 22nd November 2007, 21:40
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>

Melanie
- 23rd November 2007, 12:20
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.

rookie11
- 24th November 2007, 14:40
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.



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:


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.

rookie11
- 25th November 2007, 04:13
Can anyone provide some guidance with those 2 issues I'm having? (number generated is out of my range, and number generated always increasing)

Darrel Taylor
- 25th November 2007, 05:39
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...

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

KVLV
- 25th November 2007, 10:27
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.

Darrel Taylor
- 25th November 2007, 20:31
Someone's paying attention!

Yup you're right KVLV.

+ 1
Why do I always forget that part?

Thanks for the example too.
<br>