Discuss Scratch

Mick456
Scratcher
11 posts

Shuffling Lists

How do you make it so that the program shuffles a list without repeating any existing values (e.g. You becomes Yuu)?
dimitrip
Scratcher
500+ posts

Shuffling Lists

Hi.

Here a simple (and less performant) one.

delete [all v] of [liste2 v]
repeat until < ( length of [liste1 v] :: list) < [1] >
set ( i) to (pick random (1) to (length of [liste1 v]))
add (item (i) of [liste1 v]) to [liste2 v]
delete ( i) of [liste1 v]
end
Cheers.

Dimitri
dimitrip
Scratcher
500+ posts

Shuffling Lists

Hi again.

This one seems more efficient.
set (l) to (length of [liste v] :: list)
set (i) to (pick random [1] to (l))
set (iv) to (item (i) of [liste v])
repeat ([2]*(l))
set (j) to (pick random [1] to (l))
set (item (i) of [liste v]) to (item (j) of [liste v])
set (i) to (j)
end
set (item (j) of [liste v]) to (iv)
For others ones, see Fisher-Yates maybe.
See also more info on bias, here.

Greetz.

Dimitri

Last edited by dimitrip (Dec. 22, 2015 13:14:03)

Mick456
Scratcher
11 posts

Shuffling Lists

.

Last edited by Mick456 (Dec. 22, 2015 21:38:03)

gtoal
Scratcher
1000+ posts

Shuffling Lists

Mick456 wrote:

How do you make it so that the program shuffles a list without repeating any existing values (e.g. You becomes Yuu)?
define Shuffle
set [each v] to [0]
repeat ((length of [cards v]) - (1))
change [each v] by (1)
Swap (each) (pick random (each) to (length of [cards v]))
end

Last edited by gtoal (Dec. 28, 2015 02:52:09)

Powered by DjangoBB