Shop OBEX P1 Docs P2 Docs Learn Events
Can i Make RANDOM more Random? — Parallax Forums

Can i Make RANDOM more Random?

TomjrTomjr Posts: 2
edited 2007-05-16 13:58 in BASIC Stamp
Im currently working on what should have been a simple program that would generate a frequency through a small speaker on a bs2 stamp.· I've tried employing the Random function, but when i restart the program, it does the same tones again, they seem random, but they arent.· Also i have random plugged into the pause between the noise, and the length of the noise.
Is there a way to make a random seed? or use the time to calculate a random variable?
Please help confused.gif
PS: Dont hit me with a bunch of terms, i probably wont know them.. i'll try and understand.
Thankyou


Edit, here's the program as i have it so far:

' {$STAMP BS2}
' {$PBASIC 2.5}

Spkr·········· ·· PIN···· 15

TmAdj········ · CON···· $100
FrAdj········· · CON···· $100
R··········· ···· VAR···· Word
R2············· · VAR···· Word
FREQ········ ·· VAR···· Word
PAWZ·········· VAR···· Word
R3············· · VAR···· Word
R4·············· ·VAR···· Word
PAWZ2········· VAR···· Word


Main:
R=R+1
RANDOM R
R2=R//10+2
FREQ=R2*1000
R3=R//6+1
PAWZ=R3*1000
R4=R//10+1
PAWZ2=R4*1000
FREQOUT Spkr, PAWZ2 */ TmAdj, 1 */ FrAdj, FREQ */ FrAdj
PAUSE PAWZ
GOTO Main:

Post Edited (Tomjr) : 5/16/2007 1:46:06 PM GMT

Comments

  • DgswanerDgswaner Posts: 795
    edited 2007-05-16 13:51
    You have to do basically that. make a random seed. have your program start a loop counting to a certain number then when you press a "Go" button it takes the number from the loop and uses it as a random seed.

    or add a real time clock and do the same thing with the seconds or what ever.

    how about adding a photo resistor and using the RC time command to get a value as a random seed. this might not be different enough in a controlled environment. but just a thought.

    as I understand it there is no real random generator with out a seed. it's just a list of numbers that were randomly selected at some point. another instructor said that the government won't allow real random number generators because encryption would be unbreakable. not sure I believe that one.

    good luck.
  • Mike GreenMike Green Posts: 23,101
    edited 2007-05-16 13:54
    RANDOM isn't really random as you've noticed. It takes a value, "R" in your case, and transforms it into another value "R" in a way that, over time, behaves mathematically like a sequence of random numbers (a linear feedback shift register to be precise). To get true randomness, you have to introduce some kind of physical randomness:

    1) You could attach an external real time clock and use the time to initialize "R".

    2) You could add a "start" button and increment "R" during initialization until the button is pressed.

    3) You could add an external timer using a 555 to put out a short pulse every second or half second, then, during initialization, wait for the pulse to appear. The time between the Stamp's reset and the 555 pulse could be used to initialize "R". You want to be careful not to reset the 555 (except when the power is turned on).
  • TomjrTomjr Posts: 2
    edited 2007-05-16 13:54
    Thankyou, i will have to try that tomorrow! i'll post up again to tell you whats up

    thanks for the ideas.
    - Tommy

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·[noparse][[/noparse]IMG]http://i155.photobucket.com/albums/s305/Tomjr-/userbar4.png[noparse][[/noparse]/IMG]
  • Mike GreenMike Green Posts: 23,101
    edited 2007-05-16 13:58
    Dgswaner,
    The only true random number generators are the ones that are based on some kind of random physical process. Usually it's radioactive decay. They have a speck of some radioactive material like Polonium (used in some smoke detectors) and count the number of alpha particles emitted in a period of time (like a Geiger counter) and use that.
  • ZootZoot Posts: 2,227
    edited 2007-05-16 13:58
    The RANDOM function is a "pseudo" random generator -- it scrambles the bits of a Word but is not truly random. So upon reset you will not get truly random numbers without some kind of seed, as mentioned above.

    Running a count is OK, but that still won't give you random numbers immediately upon startup. Using a Real Time Clock or any external sensor (such as the RCTIME on a photoresistor) will give you some kind of seed right away for random numbers, e.g.

    
    Reset:
    
    Main:
        GOSUB Get_Rand
        DEBUG HOME, DEC ioWord
        GOTO Main
    
    END
    
    'Subroutines
      Get_Rand: 
         RCTIME, Pin, 1, ioWord   'read in some value from an external sensor
         RANDOM ioWord
         RETURN
    
    
    



    Edit: as usual Mike G. beats us to the punch!

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    When the going gets weird, the weird turn pro. -- HST
Sign In or Register to comment.