Shop OBEX P1 Docs P2 Docs Learn Events
Randomly selecting variables — Parallax Forums

Randomly selecting variables

boeboyboeboy Posts: 301
edited 2011-01-25 12:34 in Propeller 1
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

Comments

  • ericballericball Posts: 774
    edited 2011-01-25 10:56
    Yes: byte[ strptr ][ ?rand ** strlen ] to randomly select a character from a string.
  • MagIO2MagIO2 Posts: 2,243
    edited 2011-01-25 11:24
    Guess that's not what he wants. He wants to select one out of a number of strings.

    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.
  • JonnyMacJonnyMac Posts: 9,208
    edited 2011-01-25 11:40
    ? can return a negative value depending on the seed, so you may want to use || as well.
  • boeboyboeboy Posts: 301
    edited 2011-01-25 11:52
    MagIO2 wrote: »
    Guess that's not what he wants. He wants to select one out of a number of strings.

    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

    How do I determine string size? All my strings are different lenghts, here are some examples
       Wd1    byte "Luke Skywalker",0
       Wd2    byte "Anakin Skywalker",0
       Wd3    byte "Darth Vader",0
       Wd4    byte "The Emperor",0
       Wd5    byte "Count Dooku",0
       Wd6    byte "Battle Droid",0
       Wd7    byte "Darth Maul",0
       Wd8    byte "Padme Amidala",0
       Wd9    byte "Death Star",0
       Wd10   byte "Han Solo",0
       Wd11   byte "Hoth",0
    
    
  • MagIO2MagIO2 Posts: 2,243
    edited 2011-01-25 12:02
    It's a SPIN instruction ... ok ... my mistake, it's called strsize()
  • boeboyboeboy Posts: 301
    edited 2011-01-25 12:33
    Awsome, thank you MagIO2.
  • JonnyMacJonnyMac Posts: 9,208
    edited 2011-01-25 12:34
    The attached program may help you along if you're wanting to print a random name from that list. With a little extra processing you can ensure the entire list is used before there are any repeats (I do this with the random feature in the AP-16+ WAV player).

    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.
Sign In or Register to comment.