Discuss Scratch

Advantage_coding
Scratcher
90 posts

Need help with a math game, randomness of numbers

I'm making a math game, here's the link:
https://scratch.mit.edu/projects/729016611/
Basically, I have a code that detects which costume another sprite is using, and based on that, it sets the number1 var and the number2 var to something.

Example:

when green flag clicked
if <[operation var] = [1]> then
set [ num1 to [pick random 1 to 12]
set [ num1 to [pick random 1 to 12]
end

I did that with plus, minus and divide aswell (this one is multiplication) but it sometimes does things like 34 div by 54, which is really hard, and I don't want that. Any help appreciated a lot.
PutneyCat
Scratcher
500+ posts

Need help with a math game, randomness of numbers

For division, you just need to record one of the random numbers you use to generate the bigger number, and set the smaller number to that.

So something like:

set stored_random to (pick random 1 - 12)
set Number1 to stored_random * (pick random 1 - 12)
set Number2 to stored_random
ask what is Number1 / Number2

That way the answer will be whichever random number was picked in the second line above (not the stored_random, the other one).
RT_Borg
Scratcher
1000+ posts

Need help with a math game, randomness of numbers

Hi Advantage_coding,

Here's another explanation of what PutneyCat is suggesting, with an example (copied from a topic I answered some time back):
You can pick “good” division problems if you start by picking 2 numbers to multiply, and use the result as the dividend.

e.g.:

set [a v] to (pick random (1) to (12))
set [b v] to (pick random (1) to (12))
set [c v] to ((a) * (b))

Then if you ask: what is c divided by a, the answer will be b. And b is a nice whole number, because you picked it that way.

suppose
a = 5
b = 7
then c = 5 * 7 = 35

What is 35 / 5? It's 7. A nice whole number.
– RT_Borg
hotdoghead8789
Scratcher
2 posts

Need help with a math game, randomness of numbers

Thank u!

Any idea how to do the subtraction? I don't it to be negatives (54-89)

Any help appreciated a lot
legendary34678
Scratcher
1000+ posts

Need help with a math game, randomness of numbers

If you want to make sure that the second number is always less than the first number, you can do something like this:

repeat until <(answer :: variables) > [-1]> //Makes new numbers until the answer is greater than or equal to zero
set [first number v] to (pick random (1) to (12))
set [second number v] to (pick random (1) to (12))
set [answer v] to ((first number) - (second number))
end

Is this what you wanted?
RT_Borg
Scratcher
1000+ posts

Need help with a math game, randomness of numbers

Hi hotdoghead8789,

legendary34678's suggestion to keep trying until you find a pair that works will be ok in this specific situation, since you'll randomly find one pretty fast (that is, the repeat loop doesn't have to repeat very many times).

But if you'd like to avoid that entirely, you can:

set [a v] to (pick random (1) to (12))
set [b v] to (pick random (1) to (12))
if <(a) > (b)> then
set [first number v] to (a)
set [second number v] to (b)
else
set [first number v] to (b)
set [second number v] to (a)
end
set [answer v] to ((first number) - (second number))

– RT_Borg
Advantage_coding
Scratcher
90 posts

Need help with a math game, randomness of numbers

Thanks! That really helped.

I think I need help (again)

The code for the multiplication is

when green flag clicked
forever
if (costume #) = [3]> then
set [ num1] to (pick random (1) to (12))
set [ num2] to (pick random (1) to (12))
end
end

Then another sprite detects if your answer is right.

But it does things like 90 x 10 sometimes

Any help appreciated a lot
RT_Borg
Scratcher
1000+ posts

Need help with a math game, randomness of numbers

You have a bunch of green flag scripts. Scratch might choose any order to run them in, since they all start based on the same click event. So you're probably seeing it pick a number for addition before it changes costumes to multiplication.

You should have a green flag where it chooses the operation and then switches costumes. Then put the question selection after that (either with the blocks directly in the same script, or you can make a custom block to separate out choosing Number1 and Number2.

Let me know if you need more details.

– RT_Borg
deck26
Scratcher
1000+ posts

Need help with a math game, randomness of numbers

I think this is because you're not really controlling things properly - too many forever loops and checking things in green flag scripts. You look to be new to coding so I do realise you're probably at the early stages of learning to code but it really helps your coding if you can control things in this way.

Your operation sprite only checks the operation immediately after the green flag is clicked so once one pair of numbers is selected you don't select another. So I got 84 /7 and then 84 -1 and 84 * 7.

Much better to have a controlling script that makes sure things are reset at the start and then loops around - select an operation, show the question, wait for the right answer and then repeat from the start.

You have forever loops checking for the operation and then checking the answer if the operation is the right one. Those loops are running 30 times a second and 3 out of 4 of them are not required at that time. Much better to use a broadcast to check the result once the operation is known and stop the checking script once the answer is correct.


deck26
Scratcher
1000+ posts

Need help with a math game, randomness of numbers

So for example, in pseudo code. Let's repeat until they get 10 right answers

green flag
repeat (10)
set operation to random 1 to 4
pick numbers to match operation
calculate correct result and store in ‘correct’
repeat until correctly answered
prompt for answer (note we don't need to recalculate the correct answer all the time, just check if answer = correct)
end
end (outer repeat loop)

See the difference in terms of being in control?
hotdoghead8789
Scratcher
2 posts

Need help with a math game, randomness of numbers

More details please
RT_Borg
Scratcher
1000+ posts

Need help with a math game, randomness of numbers

What deck26 suggested looks like this.

Sprite 1

Make the Prepare A Problem custom block using “Make A Block” in “My Blocks” (all the blocks in this define could just be crammed in the spot where Prepare A Problem is used at the beginning of the “repeat 10” loop, but defining a custom block for this makes it so much easier to read and understand what's going on:

when @greenFlag clicked
repeat (10) // ask 10 different problems
Prepare A Problem::custom
broadcast [Update Operation Costume v] and wait
ask [Answer this question] and wait
repeat until <(answer) = (Correct Answer)>
say [Sorry, try again!] for (2) secs
ask [Answer this question] and wait
end
say [Correct!] for (2) secs
end

define Prepare A Problem
set [Operation: v] to (pick random (1) to (4))
if <(Operation:) = [1]> then // addition
set [Number 2 v] to (pick random (1) to (100))
set [Number 1 v] to (pick random (1) to (100))
set [Correct Answer v] to ((Number 1) + (Number 2))
end
if <(Operation:) = [2]> then // subtraction
set [Correct Answer v] to [] // be sure it repeats at least once
repeat until <(Correct Answer) \> [0]>
set [Number 1 v] to (pick random (1) to (100))
set [Number 2 v] to (pick random (1) to (100))
set [Correct Answer v] to ((Number 1) - (Number 2))
end
end
if <(Operation:) = [3]> then // multiplication
set [Number 2 v] to (pick random (1) to (12))
set [Number 1 v] to (pick random (1) to (12))
set [Correct Answer v] to ((Number 1) * (Number 2))
end
if <(Operation:) = [4]> then // division
set [Number 2 v] to (pick random (1) to (12))
set [Number 1 v] to ((Number 2) * (pick random (1) to (12)))
set [Correct Answer v] to ((Number 1) / (Number 2))
end

Operation Sprite

when I receive [Update Operation Costume v]
show
if <(Operation:) = [1]> then
switch costume to [+ v]
end
if <(Operation:) = [2]> then
switch costume to [- v]
end
if <(Operation:) = [3]> then
switch costume to [x v]
end
if <(Operation:) = [4]> then
switch costume to [/ v]
end

Compare how “in control” we are of the order things happen in, compared to the version with so many green flag scripts.

Please feel free to ask about anything that isn't clear.

– RT_Borg
Advantage_coding
Scratcher
90 posts

Need help with a math game, randomness of numbers

Thanks so much! That worked perfectly!

I figured out an easier way to do subtraction:
set [ num1] to (pick random (1) to (100))
set [ num2] to (pick random (1) to (num1))

I also added 2 extra variables: Correct answer and wrong answers. Basically, if you get it right it changes correct answers by one and the same the other way around. I have the correct answers sorted, but in which part of the code would I put change wrong answer by 1 if it's wrong?

Any help appreciated a lot!

P.S, after this the game is officially ready!
deck26
Scratcher
1000+ posts

Need help with a math game, randomness of numbers

When you get an answer it is either right or wrong so you increase one counter or the other. Probably just need an if/else block.

Last edited by deck26 (Sept. 13, 2022 07:46:15)

RT_Borg
Scratcher
1000+ posts

Need help with a math game, randomness of numbers

Advantage_coding wrote:

I have the correct answers sorted, but in which part of the code would I put change wrong answer by 1 if it's wrong?

Where it says “Sorry, try again”.
RT_Borg
Scratcher
1000+ posts

Need help with a math game, randomness of numbers

deck26 wrote:

When you get an answer it is either right or wrong so you increase one counter or the other. Probably just need an if/else block.

Advantage_coding was asking until they get the answer right, so technically someone could get the same question wrong more than once. :-)
Advantage_coding
Scratcher
90 posts

Need help with a math game, randomness of numbers

Thanks so much! The game is now ready thanks to all of your help

https://scratch.mit.edu/projects/729016611/
deck26
Scratcher
1000+ posts

Need help with a math game, randomness of numbers

HUGE improvewment in coding!

Powered by DjangoBB