Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > General > unique random numbers


Reply
 
Thread Tools Display Modes
  #1  
Old 10-25-2007, 03:49 PM
alex101uk alex101uk is offline
Newcomer
 
Join Date: Oct 2007
Posts: 1
Default 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
Reply With Quote
  #2  
Old 10-25-2007, 05:00 PM
loquin's Avatar
loquin loquin is offline
Google Hound

Retired Moderator
* Guru *
 
Join Date: Nov 2001
Location: Arizona, USA
Posts: 12,386
Default

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.
Reply With Quote
  #3  
Old 10-26-2007, 07:06 AM
Cerian Knight's Avatar
Cerian Knight Cerian Knight is offline
Multi-Technologist

Super Moderator
* Expert *
 
Join Date: May 2004
Location: Michigan
Posts: 3,751
Default

Here is an exhaustive thread on the subject, that should be read in whole:
http://www.xtremevbtalk.com/showthread.php?t=285630
__________________
"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
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->