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