Shop OBEX P1 Docs P2 Docs Learn Events
Random Number — Parallax Forums

Random Number

Special_KSpecial_K Posts: 162
edited 2013-12-05 08:06 in BASIC Stamp
Hello all,
I am working on a·bit of code·that will provide a random number. Let’s say from one to 3 and given the output number the Boe-Bot will use a different movement subroutine.
My code is this
··
RANDOM randnum················· ' random value and place in randnum
randnum=randnum //·3··········· 'Limit the result to 0-3·I think
IF randnum < 1 THEN
GOSUB Slow_Turn_Right
ELSEIF randnum > 2 THEN
GOSUB Slow_Turn_Left
ENDIF


The Boe0Bot backs up and turns left every time. Any ideas?

Comments

  • Jon WilliamsJon Williams Posts: 6,491
    edited 2006-02-03 03:02
    Well... RANDOM is not truly random, it's pseudo-random.· That's a fancy way of saying that underneath the RANDOM function is an algorithm that spits out what looks like a random value.· The thing is that it's not truly random as it is based on a function (called a linear feedback shift register)·and if you start with the same seed value you will get the same sequence of "random" numbers.·

    You might want to update your program such that as the robot·as moving forward it tumbles the RANDOM function.· Then, when an obstacle is detected, the new value is used.· The key to getting a truly random value from RANDOM is constantly calling it until some (random) external event occurs.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • Special_KSpecial_K Posts: 162
    edited 2006-02-03 04:12
    Ok I think I am a total tard. Because I have no idea haw to do that.
    Here is the entire program code.
    Where would I place the random number tumbler?
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2006-02-03 04:43
    It's not that difficult: just update the program so that when you're going forward you're also updating the RANDOM function.· When one or the other detectors senses something, use that value to make a decision.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • Special_KSpecial_K Posts: 162
    edited 2006-02-04 01:18
    You were totally right. I just moved one line and it started to give me different responses.
    thanks again.
  • coffinkcoffink Posts: 3
    edited 2013-12-04 14:16
    Hi!
    I´m all new to BASIC,
    I looked at the code and I can´t figure how to change it so the RANDOM function works...
    pleas help me and make it clear, it´s so frustrating.

    Thanks
  • coffinkcoffink Posts: 3
    edited 2013-12-04 14:23
    I´m new to BASIC stamp and I looked at this post http://forums.parallax.com/showthread.php/83215-Random-Number?highlight=random+subroutine

    I feel so dumb not figuring out how to change the code so the RANDOM function will work.
    I have tryed moving the RANDOM function lines... but no luck.
    Please help me and explain!

    Thanks!
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2013-12-04 14:26
    n // 3 returns a number between 0 and 2, not 0 and 3.

    -Phil
  • davejamesdavejames Posts: 4,047
    edited 2013-12-04 14:40
    I've merged the two threads to save the content and dump the replication.
  • tonyp12tonyp12 Posts: 1,950
    edited 2013-12-04 15:32
    If you start with the same seed every time you get the same chain of random numbers.

    The trick is to use a "random" number for the seed by using some external event such as user pressing and letting go of a button.
    if you Seed +=1 while button is pressed it would be hard for the user to always letting go of the button the same time all the time.
  • SapphireSapphire Posts: 496
    edited 2013-12-04 19:50
    Don't alter randnum with the randnum = randnum // 3 statement.

    Use a new variable to get the result in-bounds.
    seed VAR Word
    
    RANDOM seed
    randnum = seed // 3
    

    This way seed continues to be unique (within 65536 values) while randnum is restricted to 0-2.

    What you had before kept resetting the "seed" to 0, 1, or 2 and severely limited the randomness.

    Following on what tonyp12 said, you need to initialize "seed" with something at power-up. Otherwise it always starts at zero and will always produce the same pseudorandom number sequence. You could do that by timing a button press or running RCTIME on a photocell. Anything that produces different results each time.
  • Jeff GerberJeff Gerber Posts: 6
    edited 2013-12-04 21:00
    I've only had my stamp a week so I'm not of much use in that respect, though random is a very fun topic I'm familiar with from cryptography and programming. Random is actually "pseudo-random", meaning it's an algorithm that generates a series of numbers that appear to be in a random sequence but are in fact algorithmic an predictable. The "seed", as Tony and Sapphire have stated, is your key to being unpredictable and "random".

    A typical solution in computer science is to use time. By this I mean most computers have a clock and it started counting since some epoch date. If you start your program running and you are at the line where you are looking for a seed value, you can grab some part of time (say the current milliseconds, or you can do some math on the total milliseconds since the epoch, etc) and that becomes your seed value. So what does the clock represent? It represents an external input to an otherwise deterministic system. By deterministic I mean, if you start a program running and the program has no inputs to change its course of execution, it will ALWAYS do the same thing. So the really fun part about the random seed problem is that you can get quite creative with what you use for input to create a seed value. Suppose you have a switch which is "noisy"... it "bounces" when you flip the switch. You might creatively use that noise from the switch to generate a random value. The possibilities are endless but the theory is the same, you need an external input that is not predictable. With the time example, it is not predictable when someone is going to run your program to the thousandth of a second and therefore the value of the clock is anyone's guess at the point when your poll for your seed value from the clock.
  • coffinkcoffink Posts: 3
    edited 2013-12-05 08:06
    Thanks, I got it working like I wanted to.
    I use the DEBUG function to show how the RANDOM function with my modifyed

    seed VAR Word RANDOM seed randnum = seed // 3

    works.

    Now it gives me nice numbers=)
    Thanks!
Sign In or Register to comment.