How do I use the Random Function In Scratch for a Gain and a Loss

274 Views Asked by At

I want to use a random function for my code. I want the user to be able to click the Bitcoin symbol and lose anywhere from 30,000-50,000 dollars or gain anything from 30,000-50,000 dollars. I do not want to do something where it picks a random from -30,000-50,000. The number range there is too big and the chances of losing money are high. I want to have it be you either gain or you lose this much. How would I go about doing that. Simply put how do I use the random function to gain or lose money without putting such a huge range. My Current Scratch Code

3

There are 3 best solutions below

2
On BEST ANSWER

A MUCH cough cough cleaner method is:

money += (random(0, 1)*2-1)*random(30000, 50000)

All in one block!

0
On

What about something like this?

if random(0,1) is equal to 1
 money += random(30000,50000)
else
 money -= random(30000,50000)

There's a 50/50 chance of gaining, or losing, and then it will pick random. Click the checkmark if this helped.

0
On

Here's how you could write this in Scratch (For context, I am using the scratchblocks code format):

[scratchblocks]
when this sprite clicked
if <[(pick random (0) to (1))] = [0]>
change [money v] by (((-1) * ((pick random (30000) to (50000)))
else
change [money v] by ((pick random (30000) to (50000))
end
[\scratchblocks]

This is basically what it would look like in Scratch:

enter image description here

Hope this helps!