It's inefficient, but you can do this:
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!
Last edited by Harakou (2010-07-12 20:57:05)
Offline
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
Offline
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!
Offline
fullmoon wrote:
In theory, of course, the above code could execute forever
. 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
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
coolstuff wrote:
fullmoon wrote:
In theory, of course, the above code could execute forever
. Try this:
Code:
set num to (random 1 to 3) if <num = 3 > set num to 4But 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.
Last edited by Harakou (2010-07-12 21:08:12)
Offline
Harakou wrote:
coolstuff wrote:
fullmoon wrote:
In theory, of course, the above code could execute forever
. Try this:
Code:
set num to (random 1 to 3) if <num = 3 > set num to 4But 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.
![]()
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
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.
![]()
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)
Offline
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.
![]()
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
My bad Of course, that strategy will still only work if there is equal or less unwanted numbers as there is wanted numbers
Offline
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
![]()
My badOf course, that strategy will still only work if there is equal or less unwanted numbers as there is wanted numbers
![]()
Yeah, if you had multiple numbers to be excluded, it would become inefficient to write very quickly. Works great with this situation though.
Offline
Yes it is. Although I did have to experiment a bit with this. Here's a picture of how i would do it:
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
Offline
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.
Offline
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:
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:
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.
Last edited by Harakou (2010-07-12 23:23:26)
Offline
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.
![]()
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?
Offline
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.
Offline