need help with random number thingy

d0uga1

Member
Joined
Apr 13, 2005
Messages
5
Programming Experience
Beginner
can someone help me with creating an array for random numbers

i havnt got that much of a clue with these really what i need to do is produce a sequence of random numbers from 1 to 52

i need the code to pull out several random numbers that are not the same (i dont want duplicate numbers)

if there is a topic about this can you point me to right place

thanks.

this is for a card game by the way.
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,314
Location
Sydney, Australia
Programming Experience
10+
I'm guessing that you are dealing a hand of cards.

Try this:
VB.NET:
'Initialise the random number generator.  Only needs to be done once.
Randomize

'The number of cards in a hand.
Dim handSize as Integer = 5

'The list of dealt cards.
Dim dealtCardList as New ArrayList

'The next random card index generated.
Dim nextCardIndex as Integer

While dealtCardList.Count < handSize
	'Generate next card index.
	nextCardIndex = CInt(Int((52 * Rnd()) + 1))

	If Not dealtCardList.Contains(nextCardIndex)
		'Add the card to the hand.
		dealtCardList.Add(nextCardIndex)
	End If
End While

'Copy the dealt card list to an array.
Dim hand as Integer() = dealtCardList.ToArray()
 
Last edited:
Top Bottom