• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 507
  • Last Modified:

Modify code to produce unique random numbers

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
pgilfeather
Asked:
pgilfeather
  • 4
  • 3
  • 2
  • +3
4 Solutions
 
sunnycoderCommented:
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
 
arif_eqbalCommented:
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
 
sunnycoderCommented:
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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
pgilfeatherAuthor Commented:
Cheers folks,

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

Much Appreciated!

PG
0
 
Jaime OlivaresSoftware ArchitectCommented:
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
 
Jaime OlivaresSoftware ArchitectCommented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
Jaime OlivaresSoftware ArchitectCommented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
Jaime OlivaresSoftware ArchitectCommented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
pgilfeatherAuthor Commented:
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
 
RLGSCCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 4
  • 3
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now