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
How do I use the Random Function In Scratch for a Gain and a Loss
274 Views Asked by Janice En At
3
There are 3 best solutions below
0

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

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:
Hope this helps!
A MUCH cough cough cleaner method is:
money += (random(0, 1)*2-1)*random(30000, 50000)
All in one block!