Page 1 of 1

Choose randomly in a list

Posted: Mon Apr 06, 2020 2:00 pm
by kelyanok
hello
so im making a game where you need to draw something random in a list. so on mouseUp, the app needs to pick a random word to draw and display it in a field. i wanted to do something like

Code: Select all

on mouseUp
   put random(10) into rRandom
   if rRandom is x
   then
      put the x word of field "drawsList" into field "whatToDraw"
end mouseUp
without success. i wanted to add a list of things to draw in the fld "whatToDraw", and then this code chooses randomly a word in this list.
is there a way to do that? thanks

Re: Choose randomly in a list

Posted: Mon Apr 06, 2020 2:14 pm
by jmburnod
Hi kelyanok.

Something like that ?

Code: Select all

on mouseUp
   put random(10) into rRandom
   put  word rRandom of field "drawsList" into field "whatToDraw"
end mouseUp
Best regards
Jean-Marc

Re: Choose randomly in a list

Posted: Mon Apr 06, 2020 2:16 pm
by bogs
I could see a couple of ways of writing this, for instance

Code: Select all

put word(random(the number of words of field 1)) of field 1 into field 2
or

Code: Select all

put any word of field 1 into field 2

Re: Choose randomly in a list

Posted: Tue Apr 07, 2020 1:44 am
by Xero
What previous posters have said to solve your problem...
I think you've slipped up with your syntax...
kelyanok wrote:
Mon Apr 06, 2020 2:00 pm
hello
so im making a game where you need to draw something random in a list. so on mouseUp, the app needs to pick a random word to draw and display it in a field. i wanted to do something like

Code: Select all

on mouseUp
   put random(10) into rRandom
   if rRandom is x
   then
      put the x word of field "drawsList" into field "whatToDraw"
end mouseUp
without success. i wanted to add a list of things to draw in the fld "whatToDraw", and then this code chooses randomly a word in this list.
is there a way to do that? thanks
There's really no need to do the "if rRandom is x" statement. It acts as a check, and there's no need to check. You know what's in rRandom, a random number between 1 and 10, as you have just made that happen. Just make sure that the list of things you have to draw is at least 10 things long, and understand that anything after 10 won't ever get selected. There are other ways of handling the situation where the drawlist field has more or less than 10 items, such as "put random(the number of items in field drawlist) into rRandom".
The x in the "if rRandom is x" doesn't really mean anything. It's checking if the item in the container rRandom is the letter x...
In the line "put the x word of field "drawsList" into field "whatToDraw" " your words are a little out of order. if you check the other poster's syntax, it should read "put word x" not "put x word", which will read "put word 1 (or any number chosen) into field", not "put 1 word..." See the difference?
What you'll notice in the code provided by the other posters is just a compressed version of what you're doing. Getting the random number and using it all in one go.
Hope that helps.
X

Re: Choose randomly in a list

Posted: Tue Apr 07, 2020 3:04 pm
by kelyanok
hello all
thank you so much for your responses!
bogs wrote:
Mon Apr 06, 2020 2:16 pm

Code: Select all

put any word of field 1 into field 2
this one is my personal favorite for people that are asking the same question.
thanks to all!

Re: Choose randomly in a list

Posted: Tue Apr 07, 2020 3:13 pm
by bogs
Glad you liked it, I learned that one from Craig :wink:

I would suggest using the proper names of your fields, though, in my example, field 1 and field 2 are just used for expediency :D

It is a good habit to do it the right way, like this:

Code: Select all

on mouseUp
    put any word of field "drawsList" into field "whatToDraw"
end mouseUp