Shop OBEX P1 Docs P2 Docs Learn Events
Random numbers and the Propeller — Parallax Forums

Random numbers and the Propeller

WolfbrotherWolfbrother Posts: 129
edited 2010-01-19 16:18 in Propeller 1
Hi all.

I am trying to look at generating a random number to determine how many times this device I am making should spin around it's axis.· So I edited most of the motor stuff out of my code so I could post it here. What I don't get is that this thing is pretty consistent at the numbers it outputs. It starts with 5, 8, 16, 18, 6, 2, 13, 8... and then eventually settles into a pattern of 2,13 and 6. Does anyone have a clever way I can generate truly random numbers from 1 to 24 or can point the error of my ways here? What I found searching the forum was all in PASM and that's just something I don't get at all. I'm struggling with Spin.

Thanks.

Dave

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2010-01-19 02:57
    You're using the random operator ("?") improperly. It transforms its operand using a linear feedback shift register and, to get maximum randomness, you don't want to change the operand further ... change a copy of it, so you would have:[noparse][[/noparse]code]
    var long ran

    .....
    ran := cnt ' initialize ran from the clock

    x := ran? ' get the first random number
    y := ran? ' get a second random number


    ' now do whatever you want with x and y, but leave ran unchanged until the next time you need a random value.
  • mctriviamctrivia Posts: 3,772
    edited 2010-01-19 03:12
    or if you want true random values use: Real Random

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    24 bit LCD Breakout Board now in. $24.99 has backlight driver and touch sensitive decoder.

    If you have not already. Add yourself to the prophead map
  • WolfbrotherWolfbrother Posts: 129
    edited 2010-01-19 03:27
    Ok, I think I got it. I put that in the code and it does seem better.· So the idea is that I would look at it at variable times on the cnt, that's the seed for the randomizing of the ? operator and then I can just use it again whenever my previous one expires.·

    I really got kind of overwhelmed reading the object on the OBEX, because it talked about never getting really random numbers unless you used some other input. I tried to paste that code into my program, but I'm doing something wrong there too. I'm basically trying to figure this·out on the fly and really some of it isn't very intuitive to me yet. That's the file called Testing clock call for CoC 2.1, maybe I did something obvious there?

    Thanks.


    Post Edited (Wolfbrother) : 1/19/2010 3:42:29 AM GMT
  • Mike GreenMike Green Posts: 23,101
    edited 2010-01-19 04:59
    The "ran := CNT" is just done once during program initialization and gives you a new starting point for the pseudo-random sequence whenever the program begins. Whenever you want a pseudo-random number, you use "?ran" or "ran?" (use either prefix or suffix form consistently). The "?" operator takes its operand and does one cycle of the linear feedback shift register on it giving you a new 32-bit number in "ran" and as the result of the operator. The sequence of new numbers is pseudo-random (not really random, but behaves nearly randomly).
  • mctriviamctrivia Posts: 3,772
    edited 2010-01-19 05:43
    for real random put the files in the same folder then add this to your code to define it

    OBJ
    rr : "RealRandom"

    at the begining of your first function run
    rr.start

    then just substitute ran? with
    rr.random

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    24 bit LCD Breakout Board now in. $24.99 has backlight driver and touch sensitive decoder.

    If you have not already. Add yourself to the prophead map
  • WolfbrotherWolfbrother Posts: 129
    edited 2010-01-19 16:18
    Thanks to both of you for your help. I now have both versions running and they work really nice.
Sign In or Register to comment.