Randomly selecting variables
boeboy
Posts: 301
Hello all, is there any way to randomly select a byte sized string from a data block. I am building a catch phrase clone with Star Wars questions.
Thanks,
-Josh
Thanks,
-Josh
Comments
idx := ?rand**num_of_strings will give you a random number. Now you have several possibilities:
1. easy way is to loop over all strings until you reached the one with the index
str_address := @first_string
repeat idx
str_address += stringsize(str_address)+1
Then str_address points to the selected string
2. Create an array of strings
VAR
word mystrings[10]
PUB main | i
i:=0
mystring[i++]:=string( "Question1" )
mystring[i++]:=string( "Question2" )
....
Now you can access the strings via array using an index calculated the same way as in 1.
3. You can also create an address-array and all the strings in the DAT-section, but currently I don't remember exactly how. I think you need the @@ operator for that.
Difference: 1. needs runtime to search the string, 2. needs additional RAM for the array.
How do I determine string size? All my strings are different lenghts, here are some examples
Edit No change in behavior, but I changed the name of the method to nstr() as it simply prints the nth string from a list. Also added comments to method.