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

#1 2013-04-04 15:18:02

beowoulf
New Scratcher
Registered: 2013-04-04
Posts: 1

Randomizing a list

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

 

#2 2013-04-05 01:50:25

Flait7
Scratcher
Registered: 2008-04-14
Posts: 100+

Re: Randomizing a list

Could you publish a beta version of your project and post it in the comments? It would be easier to see what you mean and try to help that way.


http://scratch.mit.edu/static/projects/Flait7/2248016_sm.pnghttp://scratch.mit.edu/static/projects/Flait7/1827934_sm.png

Offline

 

#3 2013-04-05 03:56:01

Gravitation
New Scratcher
Registered: 2012-09-26
Posts: 500+

Re: Randomizing a list

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]
end
If 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".  smile

These scripts are off the top of my head, so they may not work.  tongue

Offline

 

#4 2013-04-05 03:59:15

Gravitation
New Scratcher
Registered: 2012-09-26
Posts: 500+

Re: Randomizing a list

Actually, erm, yeah, I made a stupid mistake  tongue

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]
end
at 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
tongue

Offline

 

Board footer