• AVR Freaks

Lockedsrand() % 10

Author
Guest
Super Member
  • Total Posts : 80500
  • Reward points : 0
  • Joined: 2003/01/01 00:00:00
  • Location: 0
  • Status: online
2005/12/18 02:49:22 (permalink)
0

srand() % 10

Hello there,

I have been trying to produce a random number between 0 and 9 by using srand() function. However; when I watch it on MPLAB, I see that it produces the same number each time. What may be the problem?

Do you have any idea on how to generate random numbers using C? By the way, the device is 12F675.

Any comment would be greatly appreciated. Thanks in advance.

Marv.
#1

5 Replies Related Threads

    BitWise
    Super Member
    • Total Posts : 1238
    • Reward points : 0
    • Joined: 2004/11/09 13:24:20
    • Location: UK
    • Status: offline
    RE: srand() % 10 2005/12/18 04:05:12 (permalink)
    0
    Traditionally srand() is used to set the seed used by the random number generator while rand() is used to calculate the next random number in the sequence.
    void main ()
    {
    srand (TMR0); // Done once

    for (;;) {
    int i = rand () % 10;
    }
    }

    Picking a suitable seed value for srand can tricky. If you use the values of registers (like TMR0) then it will probably be the same everytime the device is turned on. You could periodically store a random number in EEPROM and use that the next time the device is switched on or count the time between events such as button presses and reset srand periodically during operation.

    Throughout your life advance daily, becoming more skillful than yesterday, more skillful than today. This is never-ending.

    Yamamoto Tsunetomo (1659-1719)
    #2
    Ldanielrosa
    Super Member
    • Total Posts : 285
    • Reward points : 0
    • Joined: 2004/05/01 15:50:06
    • Location: Bellingham, WA. Usania
    • Status: offline
    RE: srand() % 10 2005/12/18 14:01:49 (permalink)
    0
    Ah yes, the question of random numbers comes up again. How big, how random, how fast, and how much resource can you spare for it?
    #3
    preeminent
    Junior Member
    • Total Posts : 119
    • Reward points : 0
    • Joined: 2004/06/22 14:30:57
    • Location: Beijing,Dallas,Boston,Bangkok
    • Status: offline
    RE: srand() % 10 2005/12/19 06:25:27 (permalink)
    0
    I suppose you could sample a source of white noise 8 times with the ADC, then strip off the LSB from each sample, and use those bits to form one "random" 8 bit value.

    Just a thought. grin
    #4
    lv_siddhu
    Starting Member
    • Total Posts : 62
    • Reward points : 0
    • Joined: 2005/12/26 23:21:41
    • Location: novell.com
    • Status: offline
    RE: srand() % 10 2006/01/18 03:35:42 (permalink)
    0
    You can use the combination of program counter and TMR0 for the seed
    #5
    ric
    Super Member
    • Total Posts : 25204
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    RE: srand() % 10 2006/01/18 04:52:18 (permalink)
    0
    Bitwise already pointed out that TMR0 will probably have the same count every time during startup.
    Reading the program counter (I assume you mean PCL) is also pointless, it will always return the address of the instruction doing the read!
    Biasing one of the ADC inputs up above ground, and using the lowest couple of bits from several readings would be the safest way to get a fairly random seed.
    (As preeminent suggested.)


    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #6
    Jump to:
    © 2020 APG vNext Commercial Version 4.5