I am trying to randomize items in a list for a project I am trying out. I am trying to keep tract of a card position in a deck with using a list. I keep running into a problem where there are list omissions and/or duplicates of a single list item. Is is possible to randomize a list without having any of the items in the list being omitted? or is it not even possible to do that in scratch 1.4? Also if using a list to store a set amount of data for use at a later time is not the best way to go that would be useful to know.
Offline
You'd need another, new list to be able to shuffle; an empty, unused one. Create a variable called "item", and then you can use this script:
repeat until <(length of [listyouwanttoshuffle v]) = [0]> set [item v] to (pick random (1) to (length of [listyouwanttoshuffle v])) add (item (item) of [listyouwanttoshuffle v]) to [emptyunusedlist v] delete (item) of [listyouwanttoshuffle v] end set [item v] to [0] repeat until <(length of [emptyunusedlist v]) = [0]> change [item v] by (1) add (item (item) of [emptyunusedlist v]) to [listyouwanttoshuffle v] delete (item) of [emptyunusedlist v] endIf you want to keep track of a specific item's position, create a variable called "newpos", another called "oldpos", and another one called "isselected". Then use this code instead:
set [oldpos v] to [1] //Change this to the position of the item in the list you want to keep track of
set [newpos v] to (oldpos)
set [isselected v] to [0]
repeat until <(length of [listyouwanttoshuffle v]) = [0]>
set [item v] to (pick random (1) to (length of [listyouwanttoshuffle v]))
if <(isselected) = [0]>
if <(item) < (newpos)>
change [newpos v] by (-1)
else
if <(item) = (newpos)>
set [newpos v] to ((length of [emptyunusedlist v]) + (1))
set [isselected v] to [1]
end
end
end
add (item (item) of [listyouwanttoshuffle v]) to [emptyunusedlist v]
delete (item) of [listyouwanttoshuffle v]
end
set [item v] to [0]
repeat until <(length of [emptyunusedlist v]) = [0]>
change [item v] by (1)
add (item (item) of [emptyunusedlist v]) to [listyouwanttoshuffle v]
delete (item) of [emptyunusedlist v]
end
"newpos" will contain the new position of the item that was at the position you specified in "oldpos".
Offline
Actually, erm, yeah, I made a stupid mistake
In those scripts, replace the
set [item v] to [0] repeat until <(length of [emptyunusedlist v]) = [0]> change [item v] by (1) add (item (item) of [emptyunusedlist v]) to [listyouwanttoshuffle v] delete (item) of [emptyunusedlist v] endat the end with
repeat until <(length of [emptyunusedlist v]) = [0]> add (item (1 v) of [emptyunusedlist v]) to [listyouwanttoshuffle v] delete (1 v) of [emptyunusedlist v] end
Offline