List Shuffling

BlitzMax Forums/BlitzMax Programming/List Shuffling

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

My approach is ...

Iterate through the list. If rnd()>0.3 remove current object from list than add it to the end of the list.

Example:
' randomize a list of items in a list

Global list:TList=CreateList()

Type foodtype
	Field name$
	Field price#
	Function Add(n$,p#)
		Local f:foodtype=New foodtype
		f.name$=n$ ; f.price=p
		ListAddLast list,f
	End Function
	Function ShowList()
		Print "----------------------"
		For Local f:foodtype=EachIn list
			Print f.name$+" - "+f.price
		Next
	End Function
	Function RandList(numtimes=3)
		For Local n=1 To numtimes
			For Local f:foodtype=EachIn list
				If Rnd()>0.3
					ListRemove list,f
					ListAddLast list,f
				EndIf
			Next
		Next
	End Function
End Type

SeedRnd MilliSecs()

Local f:foodtype
f=New foodtype

f.Add "pizza",2.50
f.Add "sausage",0.75
f.Add "pie",1.53
f.Add "onion",0.21
f.Add "soup",0.97
f.Add "crisps",0.29

f.ShowList
Print "~nrandom ..."
f.RandList
f.ShowList
End