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

#1 2009-04-14 15:15:11

Faldwin
Scratcher
Registered: 2009-04-14
Posts: 3

Permutations in Scratch

Is this impossible?  I want the program to pick a random number from 1-20 and then add that to a list. Then I want it to do it again, this time checking to see if the number chosen is already added to the list, and if so, choosing another one.  This way the list has no repeats.

I've been trying to make this work for awhile now, but I can't figure it out.  Any ideas, or should I use a "real" programming language?

Offline

 

#2 2009-04-14 16:22:06

BoltBait
Scratcher
Registered: 2009-03-09
Posts: 1000+

Re: Permutations in Scratch

You might be doing this backwards.

Download my blackjack game to see how the deck of cards is managed.

It needs to be able to draw every card in the deck without drawing the same card twice.

Hope this helps.


Animated sigs must be banned!
http://boltbait.com/j.pnghttp://boltbait.com/s.pnghttp://boltbait.com/d.pnghttp://boltbait.com/a.pnghttp://boltbait.com/p.png

Offline

 

#3 2009-04-14 18:39:38

Paddle2See
Scratch Team
Registered: 2007-10-27
Posts: 1000+

Re: Permutations in Scratch

Faldwin wrote:

Is this impossible?  I want the program to pick a random number from 1-20 and then add that to a list. Then I want it to do it again, this time checking to see if the number chosen is already added to the list, and if so, choosing another one.  This way the list has no repeats.

I've been trying to make this work for awhile now, but I can't figure it out.  Any ideas, or should I use a "real" programming language?

Here's one approach:

1. Build a list with 20 list elements consisting of the numbers from 1 to 20 in order.

2. Shuffle the list.  You can do this by iterating through the list and swapping each list element with the list element at some other random location in the list.

3. Now, you can just draw numbers off the list in order to produce a list of random numbers that won't repeat.

Hope that helps.


http://i39.tinypic.com/2nav6o7.gif

Offline

 

Board footer