 |
 |

10-25-2007, 03:49 PM
|
|
Newcomer
|
|
Join Date: Oct 2007
Posts: 1
|
|
unique random numbers
|
heres one for you:
i have 9 labels
each label represents a playing card
they must be randomly assigned a number between one and 11
(ace, 2, 3, 4, 5, 6, 7 ,8 ,9 ,10, Jack)
but there can be no duplicate numbers
so you could have: 3 1 5 8 6 10 2 11 4 7 9
but not: 3 1 5 3 6 10 2 3 4 7 3
i can only get it to assign random numbers with duplicates...
can someone help me out with this?
im sure i could get it if i wrote 200 odd:
if ( cardNumber1 = cardNumber2)
statements.
thanks
|
|

10-25-2007, 05:00 PM
|
 |
Google Hound
Retired Moderator * Guru *
|
|
Join Date: Nov 2001
Location: Arizona, USA
Posts: 12,386
|
|
ref the random numbers thread in the Tutor's Corner forum. (it's also linked here: Top 10 Tutorials)
Essentially, you need to generate a list of numbers in order in an array, and then 'shuffle' the data in the array. Finally, 'deal' the numbers out of the array. Since their order was shuffled randomly, they will come in random order (and, NEVER any duplicates)
Code:
' Example Code
Dim Cards(11) As Integer
Dim Card As Integer
Dim FirstCard As Integer
Dim Rnd0 As Integer
Dim Rnd1 As Integer
Dim Rnd2 As Integer
Dim N As Integer
' Populate the cards array
For N = 1 To 11
Cards(N) = N
Next N
'Shuffle the array
'Generate random number from 1 to 11
Rnd0 = Int(Rnd * 11) + 1
'Get the card number at that position and set it aside.
FirstCard = Cards(Rnd0)
' seed the shufle
Rnd1 = Rnd0
For N = 1 To 11
Rnd2 = Int(Rnd * 11) + 1
' move the card
Card = Cards(Rnd2)
Cards(Rnd1) = Cards(Rnd2)
Rnd1 = Rnd2
Next N
' Move the first card into the last spot
Cards(Rnd2) = FirstCard
' and deal
For N = 1 To 11
Debug.Print Cards(N)
Next N
The way to think of this algorithm is if you laid a row of cards down on the table, and used one hand to shuffle then.
You pull one card from a random position and set it aside. This leaves a 'hole' in the row of cards. Then, you pull a card from a second random position, and place it into the existing 'hole.' Of course, this leaves a NEW 'hole.' Repeat. After enough repititions, move the first card into the last 'hole' and you're done. Deal the deck.
|
__________________
Lou
"I have my standards. They may be low, but I have them!" ~ Bette Middler
"It's a book about a Spanish guy called Manual. You should read it." ~ Dilbert
"To understand recursion, you must first understand recursion." ~ unknown
Last edited by loquin; 10-25-2007 at 05:30 PM.
|

10-26-2007, 07:06 AM
|
 |
Multi-Technologist
Super Moderator * Expert *
|
|
Join Date: May 2004
Location: Michigan
Posts: 3,751
|
|
__________________
"May the code that you write never work in ways that you didn't expect; and may the code that you didn't write never require you to maintain it". - Ancient Chinese Proverb
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|
|
|
 |
|