# Call text from random integer selection

Get help using Construct 2

### » Wed Jul 17, 2013 4:27 pm

Hey all,

I want to generate a number between 1 and 20, and set a textbox to a value based on what number is generated.

In a hypothetical pseudo code world, I would want:

Integer= int(random(1,20))
Set Text1 = Text.Integer

I might not be going about this in the correct way. Is there any way I can put 20 values into the textbox as instance variables and count from 1 - 20?

Thanks,
-John

B
9
S
2
G
2
Posts: 47
Reputation: 1,822

### » Wed Jul 17, 2013 4:33 pm

For clarification, if I could name a text variable "1" or "2" etc. and save text to it:

Set Textbox = 1.Text
Set Textbox = 2.Text
Set Textbox = RandomInteger.Text

Is there any way to set the current key in a dictionary to a random integer? So it generates "17" and it reads the 17th key of the dictionary?
B
9
S
2
G
2
Posts: 47
Reputation: 1,822

### » Wed Jul 17, 2013 5:12 pm

Okay, sorry if my question wasn't clear.

A user touches a sprite. I want text from a textbank of 1 - 20 to appear. I have a dictionary with 20 text entries in it. How can I randomly select a line of text from the dictionary?
B
9
S
2
G
2
Posts: 47
Reputation: 1,822

### » Wed Jul 17, 2013 5:38 pm

floor(random(20)) will give you a random number in the range [0,19].

So if your dictionary keys are "text_0", "text_1", etc. you can get a random one with:

Dictionary.Get("text_" & floor(random(20)))
B
55
S
29
G
19
Posts: 1,520
Reputation: 25,870

### » Wed Jul 17, 2013 6:05 pm

okay, so I figured out what was wrong. My text variables are set as Q1, Q2, Q3, Q4, etc. I had

int(random(1,20)

that generated 1, 2, 3, 4, 5! The "Q" wasn't even being added on! So I literally wrote

Dictionary.setvalue "Q" and that did not work either. I have no idea why.

So finally I set the default value to the text as Q and just added to that. Wow. Thanks for the help Ramones
B
9
S
2
G
2
Posts: 47
Reputation: 1,822

### » Wed Jul 17, 2013 6:14 pm

Okay. I have instance variables Q1, Q2, Q3 with text saved...

Dictionary.Get("Q1") returns "0"

This program is seriously bugged. I'm not joking.
B
9
S
2
G
2
Posts: 47
Reputation: 1,822

### » Wed Jul 17, 2013 6:30 pm

http://rapidshare.com/files/3924054858/makes%20no%20sense.capx

Literally cannot imagine what I'm doing wrong
B
9
S
2
G
2
Posts: 47
Reputation: 1,822