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:

whenclickedifoperation var=1thenset num1 to [pick random 1 to 12set num1 to [pick random 1 to 12

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.:

setatopickrandom1to12setbtopickrandom1to12setctoa*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:

repeatuntilanswer>-1setfirst numbertopickrandom1to12setsecond numbertopickrandom1to12setanswertofirstnumber-secondnumberMakes new numbers until the answer is greater than or equal to zero

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:

setatopickrandom1to12setbtopickrandom1to12ifa>bthensetfirst numbertoasetsecond numbertobelsesetfirst numbertobsetsecond numbertoasetanswertofirstnumber-secondnumber

– 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

whenclickedforeverifcostume#=3>thenset num1topickrandom1to12set num2topickrandom1to12end

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:

whenclickedrepeat10PrepareAProblembroadcastUpdate Operation CostumeandwaitaskAnswer this questionandwaitrepeatuntilanswer=CorrectAnswersaySorry, try again!for2secsaskAnswer this questionandwaitsayCorrect!for2secs ask 10 different problemsdefinePrepareAProblemsetOperation:topickrandom1to4ifOperation:=1thensetNumber 2topickrandom1to100setNumber 1topickrandom1to100setCorrect AnswertoNumber1+Number2 additionifOperation:=2thensetCorrect Answerto be sure it repeats at least oncerepeatuntilCorrectAnswer>0setNumber 1topickrandom1to100setNumber 2topickrandom1to100setCorrect AnswertoNumber1-Number2 subtractionifOperation:=3thensetNumber 2topickrandom1to12setNumber 1topickrandom1to12setCorrect AnswertoNumber1*Number2 multiplicationifOperation:=4thensetNumber 2topickrandom1to12setNumber 1toNumber2*pickrandom1to12setCorrect AnswertoNumber1/Number2 division

Operation Sprite

whenIreceiveUpdate Operation CostumeshowifOperation:=1thenswitchcostumeto+ifOperation:=2thenswitchcostumeto-ifOperation:=3thenswitchcostumetoxifOperation:=4thenswitchcostumeto/

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 num1topickrandom1to100set num2topickrandom1tonum1

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