Choose randomly in a list

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: Klaus, FourthWorld, heatherlaine, kevinmiller

Post Reply
kelyanok
Posts: 22
Joined: Sun Feb 23, 2020 8:48 am
Location: Belgium

Choose randomly in a list

Post by kelyanok » 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

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2559
Joined: Sat Dec 22, 2007 5:35 pm
Location: Genève
Contact:

Re: Choose randomly in a list

Post by jmburnod » Mon Apr 06, 2020 2:14 pm

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

bogs
Posts: 4852
Joined: Sat Feb 25, 2017 10:45 pm

Re: Choose randomly in a list

Post by bogs » Mon Apr 06, 2020 2:16 pm

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
Image

Xero
Posts: 80
Joined: Sat Jun 23, 2018 2:22 pm

Re: Choose randomly in a list

Post by Xero » Tue Apr 07, 2020 1:44 am

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

kelyanok
Posts: 22
Joined: Sun Feb 23, 2020 8:48 am
Location: Belgium

Re: Choose randomly in a list

Post by kelyanok » Tue Apr 07, 2020 3:04 pm

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!

bogs
Posts: 4852
Joined: Sat Feb 25, 2017 10:45 pm

Re: Choose randomly in a list

Post by bogs » Tue Apr 07, 2020 3:13 pm

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
Image

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”