# Need Help: Randomly selecting a variable

I’m creating an app in a group for my computer class. We’ve just started coding and my role is to code the main game. In the game, a random amount of base ten blocks will appear on the screen, out of hundreds, tens, and ones. You will guess how many blocks there are (multiple choice). You will have one minute to complete as many as you can. How would I go about randomly selecting an amount of each thing? The levels also get more difficult as you go on, by the way. Hundreds and tens get introduced after maybe two levels for tens, and around five for hundreds.

How would I go about randomly selecting each, and also somehow limiting when it can select them? My teacher told me that I’ll just put an if statement and say like if it has gone through a certain amount of times then include the tens, then later the hundreds. But I don’t quite know how…

I have a month to complete this app with my teammates, then it will be submitted to the app store! I’m the only one working on the main game though.
Thanks for the help!

For the program to guess a random number, do

math.random(Largest number to choose from)

but I’m guessing your also wondering how to add the boxes, you will have to make a table like this

``````boxes = {}
for i=1, math.random(some number) do
table.insert(boxes, vec2(x,y)
end
``````

There is still more code to write, to make the boxes appear on the screen, but if you need more help, let us know!

I think this would be a bit better:

``````numbers = { -- This is a 2D table, as it has tables within itself, which is a table
{ -- Level 1
1, 2, 5 -- Numbers available for level 1
},
{ -- Level 2
1, 2, 5, 7, 10 -- Numbers available for level 2
},
{ -- Level 3
1, 2, 5, 7, 10, 12, 15 -- Numbers available for level 3
} -- etc.
}

level = 1

numberOfBoxes = math.random(numbers[level][math.random(1, #numbers[level])]

-- To simplify the above line of code:
-- optionsToChooseFrom = numbers[level] -- Get the table of numbers we can use based on the level
-- optionIndex = math.random(1, #optionsToChooseFrom) -- #table is the length of the table, math.random(min, max)
-- optionAtIndex = optionsToChooseFrom[optionIndex] -- Get the number at the index we chose, this is equivalent to numberOfBoxes
``````

A bit more complicated, though…

@SkyTheCoder I don’t get your code at all xD
@Prynok this code makes more sense to me. I understand what I need to add and all. But I still don’t get: Does the variable boxes have a sprite value, and the value of the number? Can you explain that to me a little bit

@JesssicGriffin, depends, really. Tables are not just meant for sprites, as you probably know they store information in a easy to gather way, So you could put a sprite string in there, but I don’t recommend it, as you can’t really control the sprites position. Here is a quick example of how to get the table on the screen.

``````function setup()
boxes = {}
for i=1, math.random(some number) do
table.insert(boxes, vec2(x,y)
end
end

function draw()

for i=1, #boxes do --We are creating a loop, going through the number of boxes.
sprite(somesprite, boxes[i].x, boxes[i].y) -- This will make a sprite(s) appear on-screen.
end
end
``````

Hmm, thank you @Prynok . I’ll figure it out