Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » Pick number a or number b?
- blakelarson19
-
24 posts
Pick number a or number b?
For my program I need the computer to pick a random number for my generator, but For certain instances I don't want it to pick a random number from 1 to 10, but rather I want it to pick randomly two numbers that are not right next to each other… ie. 3 or 5 … 7 or 9 … 1 or 6. Please help?
- jokebookservice1
-
1000+ posts
Pick number a or number b?
I don't quite understand. You can type whatever you want into the pick random block:
(pick random (3) to (5))Am I missing something?
- piggiety
-
65 posts
Pick number a or number b?
Oh! I get it. This might work:
This is super complicated, but I hope it helps! I didn't know what the numbers were used for, so I just put in a bunch of move … steps. You can get rid of those if you want.
when green flag clicked
set [randomizer v] to (pick random (1) to (10))
add (randomizer) to [list v]
move (randomizer) steps
set [randomizer v] to (pick random (1) to (10))
repeat until <not <<(randomizer) = ((item (1 v) of [list v]) + (1))> or <(randomizer) = ((item (1 v) of [list v]) - (1)) >>>
if <(randomizer) = ((item (1 v) of [list v]) + (1))> then
set [randomizer v] to (pick random (1) to (10))
end
if <(randomizer) = ((item (1 v) of [list v]) - (1))> then
set [randomizer v] to (pick random (1) to (10))
end
move (randomizer) steps
This is super complicated, but I hope it helps! I didn't know what the numbers were used for, so I just put in a bunch of move … steps. You can get rid of those if you want.
- blakelarson19
-
24 posts
Pick number a or number b?
Oh! I get it. This might work:when green flag clicked
set [randomizer v] to (pick random (1) to (10))
add (randomizer) to [list v]
move (randomizer) steps
set [randomizer v] to (pick random (1) to (10))
repeat until <not <<(randomizer) = ((item (1 v) of [list v]) + (1))> or <(randomizer) = ((item (1 v) of [list v]) - (1)) >>>
if <(randomizer) = ((item (1 v) of [list v]) + (1))> then
set [randomizer v] to (pick random (1) to (10))
end
if <(randomizer) = ((item (1 v) of [list v]) - (1))> then
set [randomizer v] to (pick random (1) to (10))
end
move (randomizer) steps
This is super complicated, but I hope it helps! I didn't know what the numbers were used for, so I just put in a bunch of move … steps. You can get rid of those if you want.
Thank you for the help, but I don't really understand this lol.
- blakelarson19
-
24 posts
Pick number a or number b?
I don't quite understand. You can type whatever you want into the pick random block:(pick random (3) to (5))Am I missing something?
Yes, I don't want it to pick 3 to 5 I want it to pick 3 OR 5 , so I want 4 out of the picture completely.
- piggiety
-
65 posts
Pick number a or number b?
Oh! I get it. This might work:when green flag clicked
set [randomizer v] to (pick random (1) to (10))
add (randomizer) to [list v]
move (randomizer) steps
set [randomizer v] to (pick random (1) to (10))
repeat until <not <<(randomizer) = ((item (1 v) of [list v]) + (1))> or <(randomizer) = ((item (1 v) of [list v]) - (1)) >>>
if <(randomizer) = ((item (1 v) of [list v]) + (1))> then
set [randomizer v] to (pick random (1) to (10))
end
if <(randomizer) = ((item (1 v) of [list v]) - (1))> then
set [randomizer v] to (pick random (1) to (10))
end
move (randomizer) steps
This is super complicated, but I hope it helps! I didn't know what the numbers were used for, so I just put in a bunch of move … steps. You can get rid of those if you want.
Thank you for the help, but I don't really understand this lol.
Ha! I'm not good with simple scripts…
- blakelarson19
-
24 posts
Pick number a or number b?
Oh! I get it. This might work:when green flag clicked
set [randomizer v] to (pick random (1) to (10))
add (randomizer) to [list v]
move (randomizer) steps
set [randomizer v] to (pick random (1) to (10))
repeat until <not <<(randomizer) = ((item (1 v) of [list v]) + (1))> or <(randomizer) = ((item (1 v) of [list v]) - (1)) >>>
if <(randomizer) = ((item (1 v) of [list v]) + (1))> then
set [randomizer v] to (pick random (1) to (10))
end
if <(randomizer) = ((item (1 v) of [list v]) - (1))> then
set [randomizer v] to (pick random (1) to (10))
end
move (randomizer) steps
This is super complicated, but I hope it helps! I didn't know what the numbers were used for, so I just put in a bunch of move … steps. You can get rid of those if you want.
Thank you for the help, but I don't really understand this lol.
Ha! I'm not good with simple scripts…
Should I share my project would that help?
- georgeskey
-
100+ posts
Pick number a or number b?
You could simply do:
when green flag clickedOr, to make it a little more reusable and easy,
set [number help v] to (pick random (1) to (2))
if ((number help) = (1)) then
set [number v] to {insert number here :: grey reporter}
else
set [number v] to {insert other number here :: custom reporter}
define two numbers (first number) (second number)
set [number help v] to (pick random (1) to (2))
if ((number help) = (1)) then
set [number v] to (first number)
else
set [number v] to (second number)
- blakelarson19
-
24 posts
Pick number a or number b?
You could simply do:when green flag clickedOr, to make it a little more reusable and easy,
set [number help v] to (pick random (1) to (2))
if ((number help) = (1)) then
set [number v] to {insert number here :: grey reporter}
else
set [number v] to {insert other number here :: custom reporter}define two numbers (first number) (second number)
set [number help v] to (pick random (1) to (2))
if ((number help) = (1)) then
set [number v] to (first number)
else
set [number v] to (second number)
Thank you, will the computer be able to process this quickly enough? This makes sense though and I'm trying it right now.
- georgeskey
-
100+ posts
Pick number a or number b?
And if you mean randomly pick two numbers, you can just do: You could simply do:when green flag clickedOr, to make it a little more reusable and easy,
set [number help v] to (pick random (1) to (2))
if ((number help) = (1)) then
set [number v] to {insert number here :: grey reporter}
else
set [number v] to {insert other number here :: custom reporter}define two numbers (first number) (second number)
set [number help v] to (pick random (1) to (2))
if ((number help) = (1)) then
set [number v] to (first number)
else
set [number v] to (second number)
when green flag clicked
set [first number v] to (pick random (-100) to (100))
set [second number v] to (pick random (-100) to (100))
repeat until <not <([abs v] of ((first number) - (second number))) = [1]>>
set [second number v] to (pick random (-100) to (100)
end
- zp100
-
100+ posts
Pick number a or number b?
A really easy way would be:
(((pick random (1) to (2)) * (2)) + (1))This will give either 3 or 5. Change the 1 at the end to a 3 to make it pick 5 or 7.
Last edited by zp100 (May 19, 2016 00:12:14)
- deck26
-
1000+ posts
Pick number a or number b?
Or change the second 2 to 3 and leave the 1 at the end alone. If you just want to pick between two numbers there's always a similar method but if doing different numbers throughout your project a custom block will be clearest. A really easy way would be:(((pick random (1) to (2)) * (2)) + (1))This will give either 3 or 5. Change the 1 at the end to a 3 to make it pick 5 or 7.
- zp100
-
100+ posts
Pick number a or number b?
Changing the second 2 to a 3 would make it 3 or 6. And, you could make this with a custom block. It would look like this:Or change the second 2 to 3 and leave the 1 at the end alone. If you just want to pick between two numbers there's always a similar method but if doing different numbers throughout your project a custom block will be clearest. A really easy way would be:(((pick random (1) to (2)) * (2)) + (1))This will give either 3 or 5. Change the 1 at the end to a 3 to make it pick 5 or 7.
define set var to (num1) or (num2)
set [var v] to (((pick random (1) to (2)) * ((num2::custom) - (num1::custom))) + ((num1::custom) - ((num2) - (num1))))
Last edited by zp100 (May 20, 2016 00:01:08)
- deck26
-
1000+ posts
Pick number a or number b?
My mistake, must have been distracted! Random 2 to 3 times 2 plus 1 gives 5 or 7. Apologies for any confusion.Changing the second 2 to a 3 would make it 3 or 6. And, you could make this with a custom block. It would look like this:Or change the second 2 to 3 and leave the 1 at the end alone. If you just want to pick between two numbers there's always a similar method but if doing different numbers throughout your project a custom block will be clearest. A really easy way would be:(((pick random (1) to (2)) * (2)) + (1))This will give either 3 or 5. Change the 1 at the end to a 3 to make it pick 5 or 7.define set var to (num1) or (num2)
set [var v] to (((pick random (1) to (2)) * ((num2::custom) - (num1::custom))) + ((num1::custom) - ((num2) - (num1))))
- deck26
-
1000+ posts
Pick number a or number b?
If making a custom block though much easier to just use random 1 to 2 to choose first or second.
Last edited by deck26 (May 20, 2016 07:07:35)
- drmcw
-
1000+ posts
Pick number a or number b?
or
add [3] to [random numbers v]
add [5] to [random numbers v]
set [random pick v] to (item (random v) of [random numbers v])
- zp100
-
100+ posts
Pick number a or number b?
Yes, that would probably be the simplest, but my idea doesn't require a list. oradd [3] to [random numbers v]
add [5] to [random numbers v]
set [random pick v] to (item (random v) of [random numbers v])
- blakelarson19
-
24 posts
Pick number a or number b?
Thanks guys for the advice, but earlier in the thread I found a solution,
if <touching [D1 v] ?> then
set [Numer Help v] to [(pick random (1) to (2)) ]
if <(Number Help) = [1]> then
set [Wolf Direction v] to [2 ]
if <(Number Help) = [2]> then
set [Wolf Direction v] to [4]
end
end
end
- jokebookservice1
-
1000+ posts
Pick number a or number b?
Don't nest the ifs, because if Number Help = 1, it can't become 2 before the next nested if statement Thanks guys for the advice, but earlier in the thread I found a solution,if <touching [D1 v] ?> then
set [Numer Help v] to [(pick random (1) to (2)) ]
if <(Number Help) = [1]> then
set [Wolf Direction v] to [2 ]
if <(Number Help) = [2]> then
set [Wolf Direction v] to [4]
end
end
end
- zp100
-
100+ posts
Pick number a or number b?
It would be much easier to just do Thanks guys for the advice, but earlier in the thread I found a solution,if <touching [D1 v] ?> then
set [Numer Help v] to [(pick random (1) to (2)) ]
if <(Number Help) = [1]> then
set [Wolf Direction v] to [2 ]
if <(Number Help) = [2]> then
set [Wolf Direction v] to [4]
end
end
end
if <touching [D1 v] ?> thenIt would give the exact same result, but with fewer blocks and less space.
set [Numer Help v] to ((pick random (1) to (2)) * (2))
end
- Discussion Forums
- » Help with Scripts
-
» Pick number a or number b?