Unrepeated random numbers

Miscellaneous Forums/General Discussion/Unrepeated random numbers

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.

In that case

Type card
	Field id:Int
End Type

Function Shuffle:tlist(deck:tlist)
	Local ShuffledDeck:tlist = New tlist
	Local o:Object
	
	While deck.count()
		o = deck.ValueAtIndex(Rand(deck.count()-1))
		deck.remove(o)
		ShuffledDeck.AddLast(o)
	Wend

	Return ShuffledDeck
End Function




'--Test Program
' make a deck of cards:

Local c:card
Local deck:tlist = New tlist

For Local n:Int = 1 To 52

	c = New card
	c.id = n
	deck.addLast(c)
Next


'shuffle And list

deck = Shuffle(deck)
list deck

Function List(deck:tlist)

	For Local c:card = EachIn deck
		Print c.id 
	Next	

End Function

[EDIT] changed Shuffle() to use type :object, so that it willl shuffle a list full of anything, not just cards.