Random PBP


Closed Thread
Results 1 to 15 of 15

Thread: Random PBP

  1. #1
    strujnimen's Avatar
    strujnimen Guest

    Default Random PBP

    I need code to generate random number from 1 to 7 in PICBASIC PRO. How to? Thanks.

  2. #2
    Join Date
    Feb 2006
    Location
    Gilroy, CA
    Posts
    1,530


    Did you find this post helpful? Yes | No

    Default

    Might not be the best way, but here is one way (untested):
    Code:
    random_word var word
    randomdig var byte
    
    generate_random:
       Random random_word               'generate random number and put into random_word var
       randomdig = random_word DIG 1 'read only first digit
       if randomdig = 8 then goto generate_random 'if its 8 then do over
       if randomdig = 0 then goto generate_random ' if its 0 then do over

  3. #3
    Join Date
    Feb 2006
    Location
    Gilroy, CA
    Posts
    1,530


    Did you find this post helpful? Yes | No

    Default

    Oh yeah, and filter out 9 as well.

  4. #4
    Join Date
    Aug 2006
    Location
    Omaha, Nebraska USA
    Posts
    263


    Did you find this post helpful? Yes | No

    Default

    To filter, you can also use:

    if randomdig > 7 or randomdig < 1 . . .

    Russ
    Russ
    N0EVC, xWB6ONT, xWN6ONT

    "Easy to use" is easy to say.

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


    Did you find this post helpful? Yes | No

    Default

    I need code to generate random number from 1 to 7 in PICBASIC PRO. How to? Thanks.
    PBP's RANDOM command is FAR from RANDOM.
    Makes good pink noise though ...

    But anywho ...
    Code:
        RANDOM   RandVar
        Result = RandVar ** 7 + 1
    DT

  6. #6
    Join Date
    Aug 2006
    Location
    Omaha, Nebraska USA
    Posts
    263


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Darrel Taylor View Post
    PBP's RANDOM command is FAR from RANDOM.
    Just how far from "random" is it?
    Russ
    N0EVC, xWB6ONT, xWN6ONT

    "Easy to use" is easy to say.

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


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by RussMartin View Post
    Just how far from "random" is it?
    Pretty FAR!

    It's a REPEATING sequence of 65535 numbers.

    If you start with the same "Seed" each time ...
    You will get the exact same numbers every time you run your program.

    If you start with a different seed, all that does it put you somewhere in the middle of that same repeating sequence.

    There's really nothing "RANDOM" about it.
    It's called pseudo-random, but even that's a stretch.
    <br>
    DT

  8. #8
    Join Date
    Feb 2008
    Location
    Michigan, USA
    Posts
    231


    Did you find this post helpful? Yes | No

    Default

    OK.....

    What is the best way to approach the need for the appearance of "random"?

    I have seen that there is a pattern and haven't figured out a way to circumvent it other than to make the sequence long to make it harder to see the pattern (LEDs)
    Mark

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


    Did you find this post helpful? Yes | No

    Default

    When you have a microprocessor that does the exact same thing every time you turn it on, it's very difficult to get truly "Random" numbers.

    I've seen people suggest amplifying the noise from a diode junction and reading it with the A/D. But I doubt the distribution of numbers would be very good because it ALWAYS returns to 0, and only infrequently makes it to 1023.
    So you would get many more low numbers than you would high ones.

    If you're interfacing with a Human, you can sort of get random numbers by having a timer free-running, and grab the timer value when the person presses a button. But if you need a lot of numbers really fast, that won't help either.

    I've never really seen a good method of getting truly random numbers with a PIC.
    <br>
    DT

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


    Did you find this post helpful? Yes | No

    Default

    I've designed a few items that require some form of 'Random' (search 'Electronic Dice' for a primitive example). Most applications for 'true' Random are things like Games of chance, and usually require the User to Switch-On. Nobody keeps their finger on a Button for the same length of time, and if you're simply incrementing a variable (like a Timer as Darrel suggested) until the finger is off the Button, the chance of getting the same value twice is pretty minimal. Generating Random isn't difficult, generating 'equal weighting' for something like a Dice throw is a little more tricky!

    There is a place for 'Pseudo-Random' too. Especially in Science (and Mathematics), where there is a need to be able to accurately recreate an experiment (or a series of calculations) even if they contain an element of randomness.

  11. #11


    Did you find this post helpful? Yes | No

    Default Re: Random PBP

    Quote Originally Posted by Darrel Taylor View Post
    PBP's RANDOM command is FAR from RANDOM.
    Makes good pink noise though ...

    But anywho ...
    Code:
        RANDOM   RandVar
        Result = RandVar ** 7 + 1
    This is an older post I searched because I need to generate a random number and saw you can do it with a word variable. Like this fella, I need a smaller number.

    I'm having a hard time grasping what is going on with Result = ** 7 + 1
    Huh ?

    Saw --
    ** Returns the high 16 bits of a 16-bit multiply result

    But even that isn't making sense to me.

    Has there been any updates to random where you can apply it to more than a word? -thanks

  12. #12
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,517


    Did you find this post helpful? Yes | No

    Default Re: Random PBP

    That line of code will scale the WORD sized Randvar variable (0 to 65535) to a value ranging from 1 to 7.

    Bascially it's the same as doing Result = Randvar * 7 / 65536 + 1

    /Henrik.

  13. #13
    Join Date
    Feb 2013
    Posts
    1,078


    Did you find this post helpful? Yes | No

    Default Re: Random PBP

    Use biased zener connected to ADC and sample the input

  14. #14
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,796


    Did you find this post helpful? Yes | No

    Default Re: Random PBP

    What would the range be? Near the bias?

    I think a running timer that user reads the current state maybe good enough, although sure not random since it is repeating with specific period. But the reading period is not specific or related to the timer period.

    Ioannis

  15. #15
    Join Date
    Apr 2014
    Location
    OK
    Posts
    557


    Did you find this post helpful? Yes | No

    Default Re: Random PBP

    Most of the newer PICs have Internal Temp Sensing through the ADC Module. The Temperature result would be a good source for Random Seed generation, as it will fluctuate quite a bit.

Similar Threads

  1. PBP Book
    By Bruce in forum Off Topic
    Replies: 83
    Last Post: - 4th October 2021, 13:55
  2. Random Numbers
    By BobEdge in forum mel PIC BASIC Pro
    Replies: 12
    Last Post: - 18th December 2008, 09:47
  3. Compiler differences between PBP 2.33 & 2.46
    By nikopolis in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 2nd May 2006, 20:01
  4. Newby- PBP wont compile for 18F (MPLAB)
    By jd76duke in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 18th December 2005, 00:30
  5. 6 random numbers limiting to 49 ?????
    By SuB-ZeRo in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 16th June 2005, 00:27

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