Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey all,

I've been wondering about generating random numbers within a certain range for a while.
Recently I've been using something like this a lot:
VB
Dim Generator As New Random
Generator.Next(1, 30)

(this generates numbers ranging from 1 up to and including 29)

In the past I was told to use Rnd and that I needed to call Randomize first, but because I can easily generate a random number within a range with Random I've been using that. It was long ago that I used Rnd and Randomize.

What I'm trying to do is something like this:
I want to shuffle 2 decks of cards as I load a form.
To shuffle one deck I do this:
VB
Dim DeckBefore As New Collection
Dim DeckAfter As New Collection
Dim Position As New Random
Dim ChosenCard As Integer

While DeckBefore.Count > 0
    ChosenCard = Position.Next(1, DeckBefore.Count + 1)
    DeckAfter.Add(DeckBefore.Item(ChosenCard))
    DeckBefore.Remove(ChosenCard)
End While

Imagine DeckBefore to be a list of cards like this: Ace of Spaces, Two of Spades, Three of Spades .. and so on
DeckAfter could be something like this: Jack of Hearts, Seven of Clubs, Three of Diamonds, King of Spades .. and so on

The problem I encounter is that if I use this code for 2 separate decks, they end up identically (although not the same each time). I understand this has to do with the seed value of the Random, and that is automatically picks the date/time for a seed value if I don't give it one.

All I want really is for my program to order cards "randomly" each time so that a player won't be able to predict (out of the top of his/her head) what card comes next.

any help would be appreciated.
Posted

Don't declare the Random object in the method. Try declaring it once at the class level.
 
Share this answer
 
Comments
OneInNineMillion 8-Jan-13 0:19am    
Is this because my program will eventually use more memory if I keep declaring new variables and objects each time (for example) I call a function?
Dave Kreskowiak 8-Jan-13 0:23am    
No, it has nothing to do with that. It's simply to do with how Random works. If the method this code is in is called twice, in quick succession, it's possible that the two Random objects that are created will be created with the same seed value from the system timer, thereby giving you the same string of numbers.
Sergey Alexandrovich Kryukov 8-Jan-13 0:24am    
Agree, a 5.
—SA
OneInNineMillion 8-Jan-13 0:33am    
Awesome, that solved my problem. Thanks for explaining too.
You can choose a seed of your own devising if you like, but there is no such thing as a random sequence on a computer. I think you're still safe, this is about all that any card program probably does.
 
Share this answer
 
Comments
OneInNineMillion 8-Jan-13 0:18am    
I'm not to worried about the "random sequence" not really being random. The only thing that bothers me really is that I get two identically shuffled decks of cards. I was wondering if there was an easy way to make sure the two decks end up being shuffled differently from each other. Maybe by taking the default time as a seed for one sequence and the time + 1 as a seed for the other.
Christian Graus 8-Jan-13 1:08am    
Yes, if you shuffle two decks at once, you need two different seeds. I suggest just taking xx minutes off DateTime.Now.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900