Solved

Random Numbers

Posted on 2003-12-04
2
627 Views
Last Modified: 2010-05-18
I would like to create random numbers that do not repeat. I have tried using an array like the following:


Dim Used(52) as Integer

Randomize()

redo:
        b = (Int(52 * Rnd()) + 1)

        If Used(b) = b Then
            GoTo redo
        Else
            Used2(b) = b
        End If

This portion of code does not seem to work.

Is there an easier way to create random numbers that do not repeat?
0
Comment
Question by:joey7643
2 Comments
 
LVL 1

Accepted Solution

by:
anaadoul earned 50 total points
ID: 9878270
Dim MyNums(52) as integer

Private Function DoRandomNumber as integer
Randomize()

redo:
        b = (Int(52 * Rnd()) + 1)
        for i = 0 to 52
           if MyNums(i)=b then goto redo
        next i
       DoRandomNumber=b
end function

Private sub Command1_click()
    MyNums(Pos)=DoRandomNumber
end sub

---------------------------------
There Are 10 Kind OF People
Those Who Understands Binary AND Those Who Don't
0
 
LVL 6

Expert Comment

by:prasitlee
ID: 9879048
Dear joey7643,
     Try the following code. This code would give the exact idea about unique random number generating.

Option Explicit

Private m_colUnUsedNumber As Collection

Private Sub Command1_Click()

    Dim i As Integer
    Dim j As Integer
    Dim strExtract As String
    Set m_colUnUsedNumber = New Collection
   
    For i = 0 To 52
        m_colUnUsedNumber.Add i
    Next i
   
    Label1.Caption = ""
    Do Until m_colUnUsedNumber.Count = 0
        j = (Int(m_colUnUsedNumber.Count * Rnd()) + 1)
        strExtract = m_colUnUsedNumber.Item(j)
        Label1.Caption = Label1.Caption & strExtract & " "
        m_colUnUsedNumber.Remove (j)
    Loop
   
End Sub

    I am strongly sure that this code would give you the exact answer.
                                                                             Meng
0

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question