Solved

Modify code to produce unique random numbers

Posted on 2004-08-23
15
462 Views
Last Modified: 2010-04-17
Hi,

The following code generates a random number

Sub GenerateQuestion()
Dim MyValue As Integer
Randomize   ' Initialize random-number generator.
MyValue = CInt(Int((6550 - 6546 + 1) * Rnd() + 6546))' Generate random value between 6546 and 6550
txtInput.Text=MyValue
End Sub

How can I use this code such that all the numbers it generates are unique?? I will eventually have a range of approx 100 numbers. My appliction will only require a random selection of about 20 at any one time. Hope this makes sense.

Kind Regards.
0
Comment
Question by:pgilfeather
  • 4
  • 3
  • 2
  • +3
15 Comments
 
LVL 45

Accepted Solution

by:
sunnycoder earned 125 total points
ID: 11868299
Hi pgilfeather,

I dont know much of VB but you can generate unqiue random numbers like this

For a range of 1 to n, select a prime number k  (1 <=k <= n) preferable little less than n

first random numer will be Rnd()
second will be (first_number + k)%n
third will be (second_number + k) %n
and so on

Sunnycoder
0
 
LVL 19

Assisted Solution

by:arif_eqbal
arif_eqbal earned 125 total points
ID: 11868597
Well pgilfeather
you'll have to manually check whether the Random Number generated is unique or not, you can write a function for that or just get the code from

http://www.codetoad.com/vb_random.asp

0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 11868620
No you dont have to write code to check if the number is unique ...

here is some modification of your code ... correct it according to the idea I posted above

>Generate random value between 6546 and 6550
that is random number in the range of 0 to 4 and add 6546 to get a unique number

Sub GenerateQuestion()
Dim MyValue As Integer
Dim temp as Integer
Randomize   ' Initialize random-number generator.
temp = Rnd()%5
MyValue = temp + 6546' Generate random value between 6546 and 6550

for further values, use
temp = (temp +3)%5
MyValue = temp + 6546

txtInput.Text=MyValue
End Sub

ofcourse this is subject to limitation that you have only 5 unique numbers in that range
0
 

Author Comment

by:pgilfeather
ID: 11868885
Cheers folks,

I'll have a look at all this and see if I can piece something together.

Much Appreciated!

PG
0
 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 125 total points
ID: 11870532
There is another technique:
Don't generate random numbers, you create an array with ordered numbers in a range.
Then "dis-order" the array randomly. Now you have "unique numbers" from a know range.

0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11870569
Please read some previous questions (them are in C language but the concept is the same)
http://www.experts-exchange.com/Programming/Programming_Languages/C/Q_20991403.html
http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/Q_21021480.html

If you like this technique then I can write some code for you.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 11872086
Here is an example of what jaime_olivares has described:

Option Explicit

Private Sub Command1_Click()
    Dim i As Integer
    Dim selected As Variant
   
    ' pick 20 values from 1 to 100
    selected = generateSet(1, 100, 20)
    For i = LBound(selected) To UBound(selected)
        Debug.Print i & " = " & selected(i)
    Next i
End Sub

Private Function generateSet(ByVal rangeMin As Integer, ByVal rangeMax As Integer, ByVal setSize As Integer) As Variant
    Dim rangesize As Integer
    Dim returnSet() As Integer
    Dim rangeSet() As Integer
   
    Dim i As Integer
    Dim r As Byte
    Dim swapWith As Integer
    Dim tempInt As Integer
   
    ' compute the size of the range
    rangesize = rangeMax - rangeMin + 1
   
    ' make sure the input parameters make sense...
    If rangeMax < rangeMin Then
        MsgBox "rangeMax must be greater than or equal to rangeMin"
        Exit Function
    End If
    If setSize <= 0 Or setSize > rangesize Then
        MsgBox "setsize must be greater than zero and less than or equal to the range size"
        Exit Function
    End If
   
    ' resize our arrays
    ReDim returnSet(setSize - 1)
    ReDim rangeSet(rangesize - 1)
   
    ' build the range set
    For i = 0 To rangesize - 1
        rangeSet(i) = i + rangeMin
    Next i
   
    ' shuffle the range set 7 times
    For r = 1 To 7
        ' for each item in the set,
        ' pick another item and
        ' swap them
        For i = 0 To rangesize - 1
            swapWith = Int(rangesize * Rnd)
            tempInt = rangeSet(i)
            rangeSet(i) = rangeSet(swapWith)
            rangeSet(swapWith) = tempInt
        Next i
    Next r
   
    ' build our return array
    For i = 0 To setSize - 1
        returnSet(i) = rangeSet(i)
    Next i
           
    ' return the selected set
    generateSet = returnSet
   
End Function
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11872208
Thank you Idle_Mind, but don't have to handle 2 arrays, just cut the RangeSet array before returning, using Redim Preserve. As you have posted the code, please simplify and post it again.
0
 
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 125 total points
ID: 11872360
Um...Yes Sir!     *Idle_Mind salutes smartly*

Option Explicit

Private Sub Command1_Click()
    Dim i As Integer
    Dim selected As Variant
   
    ' pick 20 values from 1 to 100
    selected = generateSet(1, 100, 20)
    For i = LBound(selected) To UBound(selected)
        Debug.Print i & " = " & selected(i)
    Next i
End Sub

Private Function generateSet(ByVal rangeMin As Integer, ByVal rangeMax As Integer, ByVal setSize As Integer) As Variant
    Dim rangesize As Integer
    Dim rangeSet() As Integer
   
    Dim i As Integer
    Dim r As Byte
    Dim swapWith As Integer
    Dim tempInt As Integer
   
    ' compute the size of the range
    rangesize = rangeMax - rangeMin + 1
   
    ' make sure the input parameters make sense...
    If rangeMax < rangeMin Then
        MsgBox "rangeMax must be greater than or equal to rangeMin"
        Exit Function
    End If
    If setSize <= 0 Or setSize > rangesize Then
        MsgBox "setsize must be greater than zero and less than or equal to the range size"
        Exit Function
    End If
   
    ' resize our array
    ReDim rangeSet(rangesize - 1)
   
    ' build the range set
    For i = 0 To rangesize - 1
        rangeSet(i) = i + rangeMin
    Next i
   
    ' shuffle the range set 7 times
    For r = 1 To 7
        ' for each item in the set,
        ' pick another item and
        ' swap them
        For i = 0 To rangesize - 1
            swapWith = Int(rangesize * Rnd)
            tempInt = rangeSet(i)
            rangeSet(i) = rangeSet(swapWith)
            rangeSet(swapWith) = tempInt
        Next i
    Next r
                 
    ' return the selected set
    ReDim Preserve rangeSet(setSize - 1)
    generateSet = rangeSet
End Function
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11872429
It is a good implementation but still the author has to accept it, anyway, it will keep to other members to review it.
BTW, maybe we are making a homework?
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 11872488
It is possible that it is homework, but based on the other questions by this author, it seems a little advanced and ambitious for a student.

Idle_Mind
0
 

Author Comment

by:pgilfeather
ID: 11879576
Hi,

thanks very much to everyone who has posted an answer to this question. I do appreciate it.

Some of the code is a little bit beyond my current abilities but I will do my best to wade through it and try and make some sense out of it.

I am learning that there may not be a simple solution for this random number issue.

Thanks

PG
0
 
LVL 8

Expert Comment

by:RLGSC
ID: 11887364
pqilfeather,

Random numbers are a rather complicated area. First, off, unless you are monitoring random galactic noise, all computer based random numbers are more properly "pseudo-random" numbers. The topic itself occupies a substantial amount of literature (one of the old classics is the 2nd volume of Knuth's Fundamentals of Programming series, "Semi-Numerical Algorithms").

The long and the short of it is this:
  - for casual use, where degree of randomness is not an issue, use the standard C library (or the similar routine available through VB). Typically, it returns a floating point number between 0 and 1. Scale this and convert this to an integer and you are done.
  - if you are doing something seriously sensitive to randomness (scientific calculations, surveys, random sampling), please dig into the literature, it is well worth the effort.

The above having been said, for simple uses, the generators which are referred to as "linear congruential" are often used behind the library entry points. They are adequate for simple applications, but have problems with randomness as things get more sensitive.

I hope that the above is helpful.

- Bob (aka RLGSC)
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now