Help with RANDOM
TonyA
Posts: 226
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
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
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
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
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
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
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