Picking a random word from a series of words

sarim123

Member
Licensed User
Longtime User
Hi everyone, have a quick question
i am trying to make an app where it picks a word from a list of words. i was thinking that the word could be picked from the list and put into a label but i really need your help.
for example: in the code, there is a series of words. Android, iOS, Windows. i would like the app to pick any one of those and put it into the top of the app. i also would like it to repick when they hit refresh.

i hope you understand all this.
Thank you all for your support
 

susu

Well-Known Member
Licensed User
Longtime User
I think you can put your words into list or array then you get random index of list/array.
 
Upvote 0

sarim123

Member
Licensed User
Longtime User
is there a guide or an example anywhere? i cant seem to find one. I am a total noob here, if you could elaborate a bit more that would be very helpful to me,

Thanks so much.
 
Upvote 0

JTmartins

Active Member
Licensed User
Longtime User
Maybe someting like

Hi sarim123,

Maybe something like this will help you out

B4X:
Sub Globals
      
   Dim words As List ' this list will hold your words

End Sub

Sub Activity_Create(FirstTime As Boolean)

   words.Initialize ' Initializes an empty list ' Here we initialize the list as empty
   
words.Addall (Array As String ("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday")) 'we add an array with 7 words to the list)

   Dim a As Int ' this variable will hold a random value

   a=Rnd (0,words.Size-1) ' between 0 and the size of the list (number of elements on the list)
   
       Dim test As String ' Let's create a variable to test
   
   test=words.Get(a) ' test will pick a string from the list based on the random number generated.

End Sub

There are no display or layouts on this code. This is just a simple example.

You could create a sub based on this code, so that the word is returned, and then show it on the screen using a label.

Hope it helps.

All the best

José
 
Last edited:
Upvote 0

sarim123

Member
Licensed User
Longtime User
Thanks so much but...

How would i get the label part working?? Is it just this?

B4X:
label1.Initialize("")
label1.Text = words
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
B4X:
label1.Initialize("")
label1.Text = test   'This is the random string selected from the list
 
Upvote 0

sarim123

Member
Licensed User
Longtime User
Words don't appear

The words do not appear, can u take a look at it, i have attached it
Thanks
 

Attachments

  • RandomWordTest.zip
    7 KB · Views: 235
Upvote 0

Mahares

Expert
Licensed User
Longtime User
@sarim:
Since you initialized the label in the designer, you do not want to initialize it again in code. Therefore remove this line:
Label1.Initialize("")
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…