Select Random Word From String (SOLVED)

J

jbit92

Guest
Hello folks.

I'm trying to have a variable select a random word from another variable; in essence it is a random word generator.

For example... I want variable "RandomWord" to select a random word from variable "WordBank" and assign it to itself.

WordBank consists of the String "wood tree slip jump...."

Anyone have a solution for this? Right now I'm stumped.

Thanks!
 
T

tserek

Guest
You could make a text file which contains these words, one below the other?

Code:
file = file_text_open_read("words.txt");
list = ds_list_create();
 
while !file_text_eof(file)
{
 ds_list_add(list,file_text_read_string(file));
 file_text_readln(file);
}
 
file_text_close(file);
 
ds_list_shuffle(list);
word = ds_list_find_value(list,0);

ds_list_destroy(list);
 
J

jbit92

Guest
Developing for android, so I don't believe I can use text files, right?

All the words are 4 letters, so is there a way I can just generate a random number that increases by 5 everytime?

Ex: "wood bark tree slip jump"... Wood starts at position 1, bark is at 6, slip at 11. So if I could generate a random number that is 1+5n and then just copy four letters that might work.
 

TsukaYuriko

☄️
Forum Staff
Moderator
Developing for android, so I don't believe I can use text files, right?
I'm not sure why you think that is the case.

From my understanding that will only work on values up to 16? What if I have over 50 values?
Then you'll either have to wait for a new GM:S update to be released (since the latest Early Access version removed the 16 argument limit) or use a list data structure. Populate it with all possible choices, then select a random one of them.
 
J

jbit92

Guest
SOLVED! Here's what I did.

Like I mentioned above, all the words are four letters and they are separated by one space. So, the start position of every word is a multiple of (1+5n). I used irandom() to generate a random number, assigned this to a variable, then used it in conjunction with string_copy().

Thanks for the help everyone!
 
Last edited by a moderator:

Fredrik

Member
Since you work with Android and not sure how if you can use a stored file or not. I'd suggest doing as mentioned above; using choose, even tho you can only have 16 or so to choose from, but you could make more lists.
Example:
Code:
list = irandom_range(1,3)
if list = 1 {word = choose(hi,no,yes,hello,ok);}
if list = 2 {word = choose(bla,haha,nty,fine);}
^ You can have 16 words for each list (or 15, not sure haha) but you can have as many lists as you'd like in the irandom_range(x1,x2);
 
Top