This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2010-07-12 20:49:31

Sunrise-Moon
Scratcher
Registered: 2009-06-27
Posts: 1000+

Pick Random from 1 - 4 but Not 3?

Is it possible to exclude a number from random?


http://i1067.photobucket.com/albums/u427/HulKDzN/RebornBlade.png

Offline

 

#2 2010-07-12 20:54:36

Harakou
Community Moderator
Registered: 2009-10-11
Posts: 1000+

Re: Pick Random from 1 - 4 but Not 3?

It's inefficient, but you can do this:

Code:

When Green Flag Clicked:
set number to 3
Repeat until <not<number = 3>>
    set number to [pick random 1 to 4]
end repeat

Edit:Ha! Outposted you this time!  tongue

Last edited by Harakou (2010-07-12 20:57:05)


http://www.blocks.scratchr.org/API.php?action=random&amp;return=image&amp;link1=http://i.imgur.com/OZn2RD3.png&amp;link2=http://i.imgur.com/duzaGTB.png&amp;link3=http://i.imgur.com/CrDGvvZ.png&amp;link4=http://i.imgur.com/POEpQyZ.png&amp;link5=http://i.imgur.com/ZKJF8ac.png

Offline

 

#3 2010-07-12 20:54:58

coolstuff
Community Moderator
Registered: 2008-03-06
Posts: 1000+

Re: Pick Random from 1 - 4 but Not 3?

I guess so:

set variable2 to 0
repeat until <variable2 = 1>
  set variable to (pick random 1 to 4)
  if <not <variable = 3> >
    set variable2 to 1
  end if
end repeat

EDIT: The poster above me is much more accurate  big_smile

Offline

 

#4 2010-07-12 20:57:01

Sunrise-Moon
Scratcher
Registered: 2009-06-27
Posts: 1000+

Re: Pick Random from 1 - 4 but Not 3?

Harakou wrote:

It's inefficient, but you can do this:

Code:

When Green Flag Clicked:
set number to 3
Repeat until <not<number = 3>>
    set number to [pick random 1 to 4]
end repeat

Works perfectly! Thanks!  smile


http://i1067.photobucket.com/albums/u427/HulKDzN/RebornBlade.png

Offline

 

#5 2010-07-12 20:59:53

fullmoon
Retired Community Moderator
Registered: 2007-06-04
Posts: 1000+

Re: Pick Random from 1 - 4 but Not 3?

In theory, of course, the above code could execute forever  big_smile . Try this:

Code:

set num to (random 1 to 3)
if <num = 3 >
  set num to 4

http://i302.photobucket.com/albums/nn100/fullmoon32/wow.jpg

Offline

 

#6 2010-07-12 21:01:50

coolstuff
Community Moderator
Registered: 2008-03-06
Posts: 1000+

Re: Pick Random from 1 - 4 but Not 3?

fullmoon wrote:

In theory, of course, the above code could execute forever  big_smile . Try this:

Code:

set num to (random 1 to 3)
if <num = 3 >
  set num to 4

But then there's a much, much higher chance of getting 4 than the others.

Offline

 

#7 2010-07-12 21:02:42

samurai768
Scratcher
Registered: 2009-07-21
Posts: 1000+

Re: Pick Random from 1 - 4 but Not 3?

The best way to do this would be with LISTS.

Add 1,2and 4 to a list and say
set [num] to (item (any) of [list)

Tada! Easy peesy.

Offline

 

#8 2010-07-12 21:06:17

Harakou
Community Moderator
Registered: 2009-10-11
Posts: 1000+

Re: Pick Random from 1 - 4 but Not 3?

coolstuff wrote:

fullmoon wrote:

In theory, of course, the above code could execute forever  big_smile . Try this:

Code:

set num to (random 1 to 3)
if <num = 3 >
  set num to 4

But then there's a much, much higher chance of getting 4 than the others.

That's what I though at first too, but it's not so! In picking a number from 1 to 4, but excluding 3, the odds of picking each number is 1 in 3. Picking any number from 1 to 3, the odds are also 1 in 3 for each integer, except that 3 will equal 4. It's brilliant, actually.  big_smile

Last edited by Harakou (2010-07-12 21:08:12)


http://www.blocks.scratchr.org/API.php?action=random&amp;return=image&amp;link1=http://i.imgur.com/OZn2RD3.png&amp;link2=http://i.imgur.com/duzaGTB.png&amp;link3=http://i.imgur.com/CrDGvvZ.png&amp;link4=http://i.imgur.com/POEpQyZ.png&amp;link5=http://i.imgur.com/ZKJF8ac.png

Offline

 

#9 2010-07-12 21:07:41

coolstuff
Community Moderator
Registered: 2008-03-06
Posts: 1000+

Re: Pick Random from 1 - 4 but Not 3?

Harakou wrote:

coolstuff wrote:

fullmoon wrote:

In theory, of course, the above code could execute forever  big_smile . Try this:

Code:

set num to (random 1 to 3)
if <num = 3 >
  set num to 4

But then there's a much, much higher chance of getting 4 than the others.

That's what I though at first too, but it's not so! In picking a number from 1 to 4, but excluding 3, the odds of picking each number is 1`in 3. Picking from 1 to 3, there is 1 in 3 odds as well, except that 3 will equal 4. It's brilliant, actually.  big_smile

I disagree.

Here's a chart of the original results vs. the final results:

1  1
2  2
3  4
4  4

Thus there is half a chance of getting four.

Offline

 

#10 2010-07-12 21:09:04

Harakou
Community Moderator
Registered: 2009-10-11
Posts: 1000+

Re: Pick Random from 1 - 4 but Not 3?

coolstuff wrote:

Harakou wrote:

coolstuff wrote:

But then there's a much, much higher chance of getting 4 than the others.

That's what I though at first too, but it's not so! In picking a number from 1 to 4, but excluding 3, the odds of picking each number is 1`in 3. Picking from 1 to 3, there is 1 in 3 odds as well, except that 3 will equal 4. It's brilliant, actually.  big_smile

I disagree.

Here's a chart of the original results vs. the final results:

1  1
2  2
3  4
4  4

Thus there is half a chance of getting four.

Ah, but he is picking a random number from 1 to 3, not 1 to 4.

Last edited by Harakou (2010-07-12 21:09:21)


http://www.blocks.scratchr.org/API.php?action=random&amp;return=image&amp;link1=http://i.imgur.com/OZn2RD3.png&amp;link2=http://i.imgur.com/duzaGTB.png&amp;link3=http://i.imgur.com/CrDGvvZ.png&amp;link4=http://i.imgur.com/POEpQyZ.png&amp;link5=http://i.imgur.com/ZKJF8ac.png

Offline

 

#11 2010-07-12 21:15:15

coolstuff
Community Moderator
Registered: 2008-03-06
Posts: 1000+

Re: Pick Random from 1 - 4 but Not 3?

Harakou wrote:

coolstuff wrote:

Harakou wrote:

That's what I though at first too, but it's not so! In picking a number from 1 to 4, but excluding 3, the odds of picking each number is 1`in 3. Picking from 1 to 3, there is 1 in 3 odds as well, except that 3 will equal 4. It's brilliant, actually.  big_smile

I disagree.

Here's a chart of the original results vs. the final results:

1  1
2  2
3  4
4  4

Thus there is half a chance of getting four.

Ah, but he is picking a random number from 1 to 3, not 1 to 4.

Oh! I totally missed that  tongue

My bad  big_smile  Of course, that strategy will still only work if there is equal or less unwanted numbers as there is wanted numbers  smile

Offline

 

#12 2010-07-12 21:19:29

Harakou
Community Moderator
Registered: 2009-10-11
Posts: 1000+

Re: Pick Random from 1 - 4 but Not 3?

coolstuff wrote:

Harakou wrote:

coolstuff wrote:


I disagree.

Here's a chart of the original results vs. the final results:

1  1
2  2
3  4
4  4

Thus there is half a chance of getting four.

Ah, but he is picking a random number from 1 to 3, not 1 to 4.

Oh! I totally missed that  tongue

My bad  big_smile  Of course, that strategy will still only work if there is equal or less unwanted numbers as there is wanted numbers  smile

Yeah, if you had multiple numbers to be excluded, it would become inefficient to write very quickly.  hmm  Works great with this situation though.  smile


http://www.blocks.scratchr.org/API.php?action=random&amp;return=image&amp;link1=http://i.imgur.com/OZn2RD3.png&amp;link2=http://i.imgur.com/duzaGTB.png&amp;link3=http://i.imgur.com/CrDGvvZ.png&amp;link4=http://i.imgur.com/POEpQyZ.png&amp;link5=http://i.imgur.com/ZKJF8ac.png

Offline

 

#13 2010-07-12 22:35:38

markyiscool
Scratcher
Registered: 2008-04-09
Posts: 100+

Re: Pick Random from 1 - 4 but Not 3?

Yes it is. Although I did have to experiment a bit with this. Here's a picture of how i would do it:

http://i992.photobucket.com/albums/af48/markk7/PickRandom.gif

where the exclusion list contins the numbers you don't want. Note that you can also change the pick random value and it will still work


Wanted the Netherlands to win or want to re-experience Spain's victory? Do it withthis!
Projects not being loved, here's an idea!

Offline

 

#14 2010-07-12 22:39:19

juststickman
Scratcher
Registered: 2009-05-31
Posts: 1000+

Re: Pick Random from 1 - 4 but Not 3?

samurai768 wrote:

The best way to do this would be with LISTS.

Add 1,2and 4 to a list and say
set [num] to (item (any) of [list)

Tada! Easy peesy.

This is the easiest way.


http://is.gd/iBQi2 Add grob to your sig and help with world dominiation!http://is.gd/iBQ9Q                                                             Hey guys, we're seriously naming our team bob?

Offline

 

#15 2010-07-12 23:10:08

Harakou
Community Moderator
Registered: 2009-10-11
Posts: 1000+

Re: Pick Random from 1 - 4 but Not 3?

markyiscool wrote:

Yes it is. Although I did have to experiment a bit with this. Here's a picture of how i would do it:

http://i992.photobucket.com/albums/af48/markk7/PickRandom.gif

where the exclusion list contins the numbers you don't want. Note that you can also change the pick random value and it will still work

This does the same thing:  big_smile

http://i30.tinypic.com/1zqbnkl.gif

samurai768 wrote:

The best way to do this would be with LISTS.

Add 1,2and 4 to a list and say
set [num] to (item (any) of [list)

Tada! Easy peesy

Hmm...A list for exclusions, or a list of acceptable numbers to pick from? I suppose it would depend on which is longer: the list of acceptable numbers, or the list of excluded numbers.

Of course, if you had a lot of different times where you had to pick a random number, and each time the numbers were different, you'd have to have a lot of lists.

I guess which method you use depends on how it's going to be used.

hmm

Last edited by Harakou (2010-07-12 23:23:26)


http://www.blocks.scratchr.org/API.php?action=random&amp;return=image&amp;link1=http://i.imgur.com/OZn2RD3.png&amp;link2=http://i.imgur.com/duzaGTB.png&amp;link3=http://i.imgur.com/CrDGvvZ.png&amp;link4=http://i.imgur.com/POEpQyZ.png&amp;link5=http://i.imgur.com/ZKJF8ac.png

Offline

 

#16 2010-07-13 02:01:36

markyiscool
Scratcher
Registered: 2008-04-09
Posts: 100+

Re: Pick Random from 1 - 4 but Not 3?

Harakou wrote:

Hmm...A list for exclusions, or a list of acceptable numbers to pick from? I suppose it would depend on which is longer: the list of acceptable numbers, or the list of excluded numbers.

Of course, if you had a lot of different times where you had to pick a random number, and each time the numbers were different, you'd have to have a lot of lists.

I guess which method you use depends on how it's going to be used.

hmm

My way works best in different scenarios while juststickman's idea will only be pratical in this scenario. What if I wanted a random number between 1 and a certain variable that changes?


Wanted the Netherlands to win or want to re-experience Spain's victory? Do it withthis!
Projects not being loved, here's an idea!

Offline

 

#17 2010-07-13 02:08:31

fullmoon
Retired Community Moderator
Registered: 2007-06-04
Posts: 1000+

Re: Pick Random from 1 - 4 but Not 3?

markyiscool wrote:

My way works best in different scenarios while juststickman's idea will only be pratical in this scenario. What if I wanted a random number between 1 and a certain variable that changes?

Well I suppose, again, that it would really depend on what you wanted to exclude.


http://i302.photobucket.com/albums/nn100/fullmoon32/wow.jpg

Offline

 

Board footer