Random number seed

1. ## Random number seed

Thank you for all our help on my last post. It all really helped. To be honest, I finially just looked at the generated number and then based on its value fired off one of 5 sub routines. Here is the chunk of code i am using....Not very elegant but it works fine.

Begin:
Count = 0
W0 = W0 + 1
Peek PORTB, B2
IF B2 = %10000000 Then Test
GoTo Begin

Test:
Count = Count + 1
IF Count = 6 Then Begin
Random W0

IF W0 < 13105 Then loop1

IF W0 < 26210 Then loop2

IF W0 < 39315 Then loop3

IF W0 < 52420 Then loop4

IF W0 <= 65525 Then loop5

loop1:
Toggle 0 'Turn on LED connected to RB0
Pause 500 'Delay for .5 seconds
Toggle 0 'Turn off LED connected to RB0
GoTo TEST

Now I am on to my latest issue. First off I am using Pic Basic (The low cost version.) I am trying to seed the random number generator with a changing opening amount. So each time I use the device, the initial random pattern changes.

My queston is, the EEprom memory specifically location "0" should keep its value after the pick is powered down and then powered up again right??

So all I have to do is set an additional counter and incrament the seed by setting memory loaction 0 to this counter.

2. Did you find this post helpful? |
Two questions:

1. In your code above, you have the following.
Code:
```IF W0 < 13105 Then loop1
IF W0 < 26210 Then loop2
IF W0 < 39315 Then loop3
IF W0 < 52420 Then loop4
IF W0 <= 65525 Then loop5```
Lets take W0=10000 then all of these IF statements will be true and be executed. Or say W0=28000; in this case last three IF statements will be executed, etc...

You should change it as follows.
Code:
```IF W0 < 13105 Then
loop1
ELSE
IF W0 < 26210 Then
loop2
ELSE
IF W0 < 39315 Then
loop3
ELSE
IF W0 < 52420 Then
loop4
ELSE
IF W0 <= 65525 Then loop5
ENDIF
ENDIF
ENDIF
ENDIF```

OR use a select case, it would make it easier to follow the logic.

2. Forget this one.

---------------------------

3. Did you find this post helpful? |
Hi,

Conclusion was Microchip offers the best routine ( assembler ... yes ! ) in its applications.

Alain

4. Did you find this post helpful? |

## Sorry to jump in.....

Hi,
Sayzer wrote:
1. In your code above, you have the following.

Code:
IF W0 < 13105 Then loop1
IF W0 < 26210 Then loop2
IF W0 < 39315 Then loop3
IF W0 < 52420 Then loop4
IF W0 <= 65525 Then loop5

Lets take W0=10000 then all of these IF statements will be true and be executed. Or say W0=28000; in this case last three IF statements will be executed, etc...
Why is that?
If W0=10000 the first IF statement tests true and the program jumps to the Loop1 routine which is executed. At the end of Loop1 the program jumps to Test where a new RANDOM is executed and assigned to W0. Lets say it's 28000 as in you example. The third statement tests true and the program jumps to Loop3, executes it and then starts over with another RANDOM. At least that's how I read it.

HTH
/Henrik Olsson.

5. Did you find this post helpful? |
You seem to be having it correct Henrik.

6. Did you find this post helpful? |

## Random number seed

Actually the if statements work fine, but I like adding the "Else" statements and "EndIf"s to clean up the code (Thank you Sazer). and comments to clarify the logic behind my code. My real issue is the way the code is now, my code always provides the same sequence of numbers on start up. I need it to be different each time at startup as well as any other time it basically is supposed to randomly flash 1 of 5 different LEDs - 5 times and then stop and wait for B.7 to go high and then star again. So how can I randomly add a seed to the very first iteration of this process. I read some where that you can pull a value from TMRO, so it will give you an initial seed of 0 to 255. Any thoughts?
Last edited by kwelna; - 24th January 2007 at 03:08.

7. Did you find this post helpful? |
Hi, Kwelna

Just keep the last value before switching off in EEPROM ... as a new seed !!!

Alain

8. Did you find this post helpful? |

## Random seed

Duh!!! So simple I never would have thought of it.........well maybe in a week or two.... I am alwqys looking for a more complicated method. that is what happens when you get old!

Thanks again

Kevin

9. Did you find this post helpful? |
Originally Posted by kwelna
that is what happens when you get old!
Imagine when you'll be as old as i am

You could still use an hardware solution... count pulses at the output of a white (even pink) noise generator... now it's going to be really random. Always a long debate around this....

10. Did you find this post helpful? |

## Stone age survivors ???

Hi, Steve

You were talking about the MM 5837 ... I suppose ???

The only one corresponding to your age LOL !!!

Hi, Kevin

<< I am alwqys looking for a more complicated method. that is what happens when you get old! >>

Here, you're only talking about yourself ...

Ah, Will you have a look here ???

http://www.manhattancontrols.com/for...932b79b20ee1c5

Alain
Last edited by Acetronics2; - 1st February 2007 at 10:38.

11. Did you find this post helpful? |
Originally Posted by Acetronics
Hi, Steve

You were talking about the MM 5837 ... I suppose ???

The only one corresponding to your age LOL !!!
LMAO! well could also be a simple op-amp (maybe dual), cmos gate etc etc. no need for a dedicated IC. Maybe PBP SOUND could do the job.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts