CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 2002
    Location
    United Kingdom
    Posts
    60

    How can I generate numbers w/out duplicates...

    Hi all,

    I want to be able to generate 6 random numbers from 1 to 20 and place each of those numbers into a label(of which there are 6).

    But each number must be unique, i.e. no duplicates.

    Any possible code for this???

    Thanks

    Mark

  2. #2
    Join Date
    Sep 2001
    Location
    Québec, Canada
    Posts
    1,923
    The first technic would be to generated a random number, look for an array to see if it has already been generated, and continue like that.

    But a better method would be to generate an array of 20 (with the number from 1 to 20) and shuffle the array (you know, swap elements) a X number of times, and after just take the number from indice 0 to 5, look at this example:

    http://www.codeguru.com/forum/showth...&threadid=5650

    JeffB - hope it helps
    CodeGuru VB FAQ Visual Basic Frequently Asked Questions
    VB Code color Tool to color your VB code on CodeGuru
    Before you post Importants informations to know before posting

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured