Shop OBEX P1 Docs P2 Docs Learn Events
Help with RANDOM — Parallax Forums

Help with RANDOM

TonyATonyA Posts: 226
edited 2005-09-05 17:06 in BASIC Stamp
Hi,


I am trying to select values randomly, from data tables. How could I modify the following code to make this happen?

Suppose I have this data table:

' C D E F G A B
'
C_Scale DATA 001,002,004,005,007,009,011
DATA 012,014,016,017,019,021,023
DATA 024,026,028,029,031,033,035
DATA 036,038,040,041,043,045,047
DATA 048,050,052,053,055,057,059
DATA 060,062,064,065,067,069,071
DATA 072,074,076,077,079,081,083
DATA 084,086,088,089,091,093,095
DATA 096,098,100,101,103,105,107
DATA 108,110,112,113,115,117,119
DATA 120,122,124,125,127,$FF,$FF

And this:

Main:

DO
GOSUB Get_keyofC
GOSUB Get_keyofCoctave
READ (C_Scale + (octave * 12) + note), noteOut ' plays all notes in C scale with octaves.
IF (noteOut <= 127) THEN
SEROUT MidiOut, Baud, [noparse][[/noparse]NoteOn, noteOut, velocity]
ENDIF
LOOP
END

And this:

'
[noparse][[/noparse] Subroutine ]
Key of C Note Selection

Get_keyofC:
HIGH NoteCtrl_1
PAUSE 1
RCTIME NoteCtrl_1, 1, result
note = result / 53 ' convert to 0..11
Return



How could I read the above table chart values using the RANDOM command?

Thanks for any insight.

Tony A

Post Edited (TonyA) : 9/4/2005 10:31:29 PM GMT

Comments

  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-09-04 22:45
    It's easy -- use RANDOM.·RANDOM uses a 16-bit value (0 - 65535) and you want to get from that a value between zero and notes-1.· The answer (drum roll, please)... my favorite operator: // (modulus).· This code fragment will tumble the random value (necessary for true randomness) until a button is pressed, then will select one of 128 notes:

    Main
    · DO
    ··· RANDOM rndVal····················· ' tumble·random value
    ··LOOP UNTIL (Key = Pressed)·········· '·· until key is pressed

    · theNote = rndVal // 128············· ' get randome note, 0 - 127
    · READ·C_Scale + theNote, noteVal··· · ' read value from table

    At this point you can do something with noteVal.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • TonyATonyA Posts: 226
    edited 2005-09-04 23:15
    Hi Jon,

    Thank you. I was just wondering though. Since I have the data tables all written out could I do this too:



    Get_keyofC: 'subroutine
    HIGH NoteCtrl_1
    PAUSE 1
    RCTIME NoteCtrl_1, 1, result ' read key of C pin 1
    note = result / 53 ' convert to 0..11
    RANDOM note ' make notes random
    RETURN

    result = note
    RANDOM note

    Since I only have a specified number of notes in the table named "C_Scale", would this randomly select notes in that table?

    C_Scale DATA 001,002,004,005,007,009,011
    DATA 012,014,016,017,019,021,023
    DATA 024,026,028,029,031,033,035
    DATA 036,038,040,041,043,045,047
    DATA 048,050,052,053,055,057,059
    DATA 060,062,064,065,067,069,071
    DATA 072,074,076,077,079,081,083
    DATA 084,086,088,089,091,093,095
    DATA 096,098,100,101,103,105,107
    DATA 108,110,112,113,115,117,119
    DATA 120,122,124,125,127,$FF,$FF

    Main:

    DO
    GOSUB Get_keyofC
    READ (C_Scale + (octave * 12) + note), noteOut ' plays all notes in C scale with octaves.
    IF (noteOut <= 127) THEN
    SEROUT MidiOut, Baud, [noparse][[/noparse]NoteOn, noteOut, velocity]
    PAUSE rhythm
    ENDIF
    LOOP
    END

    Post Edited (TonyA) : 9/4/2005 11:27:43 PM GMT
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-09-05 13:04
    You can do that, but the program will behave the same every time -- seemingly random, but actually not. To prove it to yourself, run this simple program:

    rndVal··· VAR··· Word
    idx·······VAR··· Nib

    Main:
    · DEBUG CLS
    · FOR idx = 1 TO 5
    ····RANDOM rndVal
    ··· DEBUG DEC rndVal, CR
    · NEXT
    · END

    Note that every time you run the program you get the same list of numbers -- this is the nature of psuedo-random number generators; they're mathmatically based, and not truly random.· By adding some external event you can get true randomness.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • TonyATonyA Posts: 226
    edited 2005-09-05 13:39
    Hi Jon,

    Thanks. Very interesting.

    What if the external event was the act of pushing a button? Would it become more random? (Since when you press a button, you will never be able to press it in a uniform way).

    I understand what you're saying about the math, it would be inherently patterned. I also have to read more about "modulus".

    Thanks again,
    Tony
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-09-05 17:06
    Yes, that's exactly what I demonstrated in my post above -- this works because you don't know the button press is going to come. That said, you must constantly tumble the RANDOM generator until it does. This is usually not a problem; just include the RANDOM line somewhere in a loop that causes code to be repeated.

    Modulus is simple: It returns the remainder of a division. The KEY is that // will always return a value between 0 and N-1, where N is the divisor used.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
Sign In or Register to comment.