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

#1 2012-09-11 17:29:47

bullelk12
Scratcher
Registered: 2012-05-26
Posts: 100+

Randomizing a list

I need to know how to take an existing list and shuffle all of its contents around randomly. Is it possible?


http://mag.racked.eu/cimage/i6000/Achievement++get%21/Scratcher+love+minecraft%21/mca.png

Offline

 

#2 2012-09-11 17:42:47

bullelk12
Scratcher
Registered: 2012-05-26
Posts: 100+

Re: Randomizing a list

I need to know how to take an existing list and shuffle all of its contents around randomly. Is it possible?

2nd post


http://mag.racked.eu/cimage/i6000/Achievement++get%21/Scratcher+love+minecraft%21/mca.png

Offline

 

#3 2012-09-11 17:47:28

jvvg
Scratcher
Registered: 2008-03-26
Posts: 1000+

Re: Randomizing a list

There is a way to do it. Pretty much just randomly swap items a bunch of times.


http://tiny.cc/zwgbewhttp://tiny.cc/e1gbewhttp://tiny.cc/zygbewhttp://tiny.cc/izgbew
Goodbye, Scratch 1.4  sad                                                        Hello Scratch 2.0!  smile

Offline

 

#4 2012-09-11 18:11:37

zammer990
Scratcher
Registered: 2012-01-22
Posts: 500+

Re: Randomizing a list

when gf clicked
repeat (length of [list v])
set [selector v] to (pick random (1) to (length of [list v]))
add (item (selector) of [list v]) to [scrambled list v]
delete (selector) of [list v]
end

Last edited by zammer990 (2012-09-11 18:12:02)


http://i45.tinypic.com/2ynq7nn.jpg Play now!

Offline

 

#5 2012-09-11 18:12:09

Prestige
Scratcher
Registered: 2008-12-15
Posts: 100+

Re: Randomizing a list

Yeah it is possible  smile


repeat (large number, eg 100)
set [random v] to (pick random (1) to (length of [list v]))
set [value v] to (item (random) of [list v])
delete (random) of [list v]
insert (value) at (random) of [list v]
end
Another, more complicated, way of doing it would be this:
(This might not be the most efficient way of doing it (it uses 3 lists), but it will work)  smile


repeat (length of [list v])
set [random v] to (pick random (1) to (length of [list v]))
repeat until <not <[randomising v] contains (random)>>
set [random v] to (pick random (1) to (length of [list v]))
end
add (random) to [randomising v]
end
set [random v] to (1)
repeat (length of [list v])
add (item (item (random) of [randomising v]) of [list v]) to [shuffle v] 
change [random v] by (1)
end
delete [all v] of [list v]
set [random v] to (1)
repeat (length of [shuffle v])
add (item (random) of [shuffle v]) to [list v]
end 
end

Last edited by Prestige (2012-09-11 18:23:21)


"Don't insult someone until you've walked a mile in their shoes. That way, if they don't like what you have to say, you'll be a mile away and still have their shoes  smile  "

Offline

 

#6 2012-09-11 18:31:54

bullelk12
Scratcher
Registered: 2012-05-26
Posts: 100+

Re: Randomizing a list

ok, I'll try em.


http://mag.racked.eu/cimage/i6000/Achievement++get%21/Scratcher+love+minecraft%21/mca.png

Offline

 

#7 2012-09-11 18:37:44

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

Re: Randomizing a list

I've got a demo project that uses insert to build up a random sequential list - see if this helps at all

http://scratch.mit.edu/projects/Paddle2SeeFixIt/958900


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

Offline

 

#8 2012-09-11 21:04:00

Molybdenum
Scratcher
Registered: 2012-06-17
Posts: 1000+

Re: Randomizing a list

Was the OP deleted? I thought it was someone asking for help.


"The Enrichment Center is required to remind you that you will be baked, and then there will be cake."
(|Balls and Platforms: Stay on!|) (|NaOS-H: An operating system... Or is it?|)

Offline

 

#9 2012-09-11 21:05:19

jvvg
Scratcher
Registered: 2008-03-26
Posts: 1000+

Re: Randomizing a list

Molybdenum wrote:

Was the OP deleted? I thought it was someone asking for help.

It was moved to help with scripts.

Now I am the topic owner.  tongue


http://tiny.cc/zwgbewhttp://tiny.cc/e1gbewhttp://tiny.cc/zygbewhttp://tiny.cc/izgbew
Goodbye, Scratch 1.4  sad                                                        Hello Scratch 2.0!  smile

Offline

 

#10 2012-09-12 00:09:56

bullelk12
Scratcher
Registered: 2012-05-26
Posts: 100+

Re: Randomizing a list

I figured it out, sorry, the ideas didn't work.


http://mag.racked.eu/cimage/i6000/Achievement++get%21/Scratcher+love+minecraft%21/mca.png

Offline

 

#11 2012-09-16 06:37:47

TPAL125
Scratcher
Registered: 2012-06-09
Posts: 18

Re: Randomizing a list

Hmm... I just hope I can explain this properly.

You pick the first item in the list and remember the place it was in, then you pick a random number between 1 and the length of the list. You remember what that item was, then you swap that with the first item you chose, and the same for the other one.

Or download this and look at the scripts:
http://scratch.mit.edu/projects/TPAL125/2783196

Offline

 

#12 2012-09-16 07:06:58

zammer990
Scratcher
Registered: 2012-01-22
Posts: 500+

Re: Randomizing a list

TPAL125 wrote:

Hmm... I just hope I can explain this properly.

You pick the first item in the list and remember the place it was in, then you pick a random number between 1 and the length of the list. You remember what that item was, then you swap that with the first item you chose, and the same for the other one.

Or download this and look at the scripts:
http://scratch.mit.edu/projects/TPAL125/2783196

The method I posted would be a simpler one.


http://i45.tinypic.com/2ynq7nn.jpg Play now!

Offline

 

#13 2012-09-16 07:12:16

zammer990
Scratcher
Registered: 2012-01-22
Posts: 500+

Re: Randomizing a list

another method:

when gf clicked
repeat (scrambling amount)
set [chooser v] to (pick random (1) to (length of [list v]))
set [chooser2 v] to (pick random (1) to (length of [list v]))
set [temp v] to (item (chooser) of [list v])
replace item (chooser) of [list v] with (item (chooser2) of [list v])
replace item (chooser2) of [list v] with (temp)
Is this solved?


http://i45.tinypic.com/2ynq7nn.jpg Play now!

Offline

 

#14 2012-09-16 09:24:06

Molybdenum
Scratcher
Registered: 2012-06-17
Posts: 1000+

Re: Randomizing a list

zammer990 wrote:

when gf clicked
repeat (length of [list v])
set [selector v] to (pick random (1) to (length of [list v]))
add (item (selector) of [list v]) to [scrambled list v]
delete (selector) of [list v]
end

This is the easiest, just remember to delete all of scrambled list before you start.


"The Enrichment Center is required to remind you that you will be baked, and then there will be cake."
(|Balls and Platforms: Stay on!|) (|NaOS-H: An operating system... Or is it?|)

Offline

 

#15 2012-09-16 10:11:48

zammer990
Scratcher
Registered: 2012-01-22
Posts: 500+

Re: Randomizing a list

Molybdenum wrote:

zammer990 wrote:

when gf clicked
repeat (length of [list v])
set [selector v] to (pick random (1) to (length of [list v]))
add (item (selector) of [list v]) to [scrambled list v]
delete (selector) of [list v]
end

This is the easiest, just remember to delete all of scrambled list before you start.

And add the items you want to be scrambled at the start, Assuming it's not user inputted


http://i45.tinypic.com/2ynq7nn.jpg Play now!

Offline

 

#16 2012-09-17 08:17:53

TPAL125
Scratcher
Registered: 2012-06-09
Posts: 18

Re: Randomizing a list

zammer990 wrote:

TPAL125 wrote:

Hmm... I just hope I can explain this properly.

You pick the first item in the list and remember the place it was in, then you pick a random number between 1 and the length of the list. You remember what that item was, then you swap that with the first item you chose, and the same for the other one.

Or download this and look at the scripts:
http://scratch.mit.edu/projects/TPAL125/2783196

The method I posted would be a simpler one.

Mine works as well, though.

Offline

 

#17 2012-09-18 02:41:51

bullelk12
Scratcher
Registered: 2012-05-26
Posts: 100+

Re: Randomizing a list

Yours would work as well, that's true.


http://mag.racked.eu/cimage/i6000/Achievement++get%21/Scratcher+love+minecraft%21/mca.png

Offline

 

Board footer