[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
Solved

# Modify code to produce unique random numbers

Posted on 2004-08-23
Medium Priority
479 Views
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
Question by:pgilfeather
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 4
• 3
• 2
• +3

LVL 45

Accepted Solution

sunnycoder earned 500 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

arif_eqbal earned 500 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

0

LVL 45

Expert Comment

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

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

Jaime Olivares earned 500 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

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

LVL 86

Expert Comment

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

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 86

Assisted Solution

Mike Tomlinson earned 500 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

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 86

Expert Comment

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

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

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

Question has a verified solution.

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

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is â€“ how do I become a web developer?
This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to goâ€¦
Six Sigma Control Plans
###### Suggested Courses
Course of the Month12 days, 17 hours left to enroll