Solved

# non repeating random numbers

Posted on 2003-03-19
Medium Priority
456 Views
i am trying to produce ten seperate non repeating numbers in an array of labels and am not sure how to do this, i can create ten random numbers but sometimes they repeat which i want to stop happening. please help, if you need any information just email me. thanks x
0
Question by:Lainey_bloggs
[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
• +7

LVL 2

Expert Comment

ID: 8167619
Ok, to do this, here is a fairly slow code that will do it.

dim inNum(1 to 10) as integer

For inI = 1 to 10
inNum(inI) = int(rnd*11)
if inI > 1 then
For inJ = 1 to inI - 1
if inNum(inI) = inNum (inJ) then
inJ = inI - 1
inI = inI - 1
end if
Next inJ
Next inI

0

LVL 2

Expert Comment

ID: 8167625
Here's an ammendment to the above.....It works

Private Sub Command1_Click()
Dim inNum(1 To 10) As Integer
Randomize
For inI = 1 To 10
inNum(inI) = Int(1 + Rnd * 10)
If inI > 1 Then
For inJ = 1 To inI - 1
If inNum(inI) = inNum(inJ) Then
inJ = inI - 1
inI = inI - 1
End If
Next inJ
End If
Next inI

For inI = 1 To 10
Picture1.Print inNum(inI)
Next inI
End Sub
0

LVL 3

Expert Comment

ID: 8167680
use Randomize.

Private Sub Form_Click()

Dim RandomNum As Integer, LowerRange As Integer, UpperRange As Integer
LowerRange = 1
UpperRange = 100

Randomize
RandomNum = Int((UpperRange * Rnd) + LowerRange)

Caption = RandomNum

End Sub
0

LVL 3

Expert Comment

ID: 8167690
nevermind that, that's not what you asked for :-P
0

LVL 5

Expert Comment

ID: 8167907
i think theres no way, with an existing function like rnd and Randomize.... It always have a chance that the number repeat cause its random. The only way to not repeat its to set number, or increment them

a global var with increment in your function

0

LVL 22

Expert Comment

ID: 8168237
There are actually several ways, but it depends on the extend of the rules.

The brute-force method is to simply keep track of each number as it's generated, then scan all previous numbers to ensure that the current one doesn't repeat.

An easier way is to pre-assign all possible numbers and shuffle them.

For example, when dealing with a deck of cards, you have 52 faces but you want them randomly selected.  To handle this, simply place them all into an array/stack and shuffle them:

Dim intCards(51) as integer
dim intCardCntr as integer
dim intTempPick as integer
dim intTempCard as integer

' create the card deck
for intCardCntr = 0 to 51
next intCardCntr

' Shuffle the deck by going through each card and randomly swapping it with another card
for intCardCntr= 0 to 51
intTempPick = int(rnd * 52)' pick an index to swap with
intTempCard = intCard(intCardCntr) ' save the current card
intCard(intCardCntr) = intCard(intTempPick) ' swap the randomly selected card to this location
intCard(intTempPick) = intTempCard' place the original card at its new location
next intCardCntr

The deck will now be shuffled.  You can do this with any predefined set of values by storing them n an array then shuffling them.
0

LVL 3

Expert Comment

ID: 8168303
Private Sub Command1_Click()

Dim RandomNum As Integer, LowerRange As Integer, UpperRange As Integer

LowerRange = -10
UpperRange = 10

ReDim temparray(LowerRange To UpperRange)
For i = LowerRange To UpperRange
temparray(i) = CStr(i)
Next

Randomize

Number = 10 'ten seperate non repeating numbers
ReDim MyNumbers(Number - 1)
i = 0
Randomize
While i < Number
RandomNum = Int(((UpperRange - LowerRange) * Rnd) + LowerRange)

If temparray(RandomNum) <> "" Then

MyNumbers(i) = temparray(RandomNum)
temparray(RandomNum) = ""
i = i + 1

End If

Wend

For i = 0 To Number - 1
Debug.Print i; MyNumbers(i)
Next

End Sub

0

LVL 3

Expert Comment

ID: 8168305
Isn't there a mathematical formula that does this? I ran out of time this evening scanning the links but if you use Google to search for "non repeating random number series algorithm" it comes up with all sorts of interesting stuff...

(Personally, I'd stick with Jacamar's program... :-) )
0

LVL 22

Accepted Solution

rspahitz earned 100 total points
ID: 8168644
In some cases, you can create a non-repeating sequence, but it will hardly be random:

For example, if you take the sequence 0 to 6 and divide each by 7, the any specific decimal position appears to be random:
0/7=.000000
1/7=.142857
2/7=.285714
3/7=.428571
4/7=.571428
5/7=.714285
6/7=.857142

If you look at the second decimal digit, your sequence is 0,4,8,2,7,1,5
If you look at the third decimal digit, your sequence is 0,2,5,8,1,4,7
If you look at the 3rd and 4th, you get: 00, 28, 57, 85, 14, 42, 71

When you do something similar with a prime number, you often get such sequences.  If you "randomly" select a good candidate for this process, the sequence can appear to be random although it will eventually repeat.

0

LVL 5

Expert Comment

ID: 8169670
Do you just need 10 random numbers? If so, you could use the code below. It takes a string of digits from 0 to 9, and selects one randomly, and then removes it from the string.

Kindest regards,
Rhaedes

Randomize Timer
myString = "0123456789"

For n = 0 To 9
myrandomnumber = Mid\$(myString, 1 + Int(Rnd * Len(myString)), 1)
myString = Replace(myString, myrandomnumber, "")
MsgBox myrandomnumber
'Or if you need values from 1 to 10 use
'MsgBox Val(myrandomnumber) + 1
Next n
0

Expert Comment

ID: 8187115
ok..here it goes

Dim numbers(1 to 10) as integer
Dim x as integer

Command1_Click()

Randomize
For x = 1 to 10

numbers(x) = int(rnd * 1000) 'you can change 1000 to the largest number allowed in the set of 10..

label1(x).caption = numbers(x)

Next

' you need to have an array of 10 labels on your form already.  and a command button where you place the code.
0

Expert Comment

ID: 8531990
Hi Lainey_bloggs,
This old question (QID 20556264) needs to be finalized -- accept an answer, split points, or get a refund.  Please see http://www.cityofangels.com/Experts/Closing.htm for information and options.
0

LVL 6

Expert Comment

ID: 8957338
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

GPrentice00
EE Cleanup Volunteer
0

LVL 6

Expert Comment

ID: 8957340
DeAn's post although apparently used to answer a different question can easily be adapted to do exactly as desired, with comments
0

LVL 22

Expert Comment

ID: 8962188
DeAn's comment does NOT answer the question.  It offers a random number which COULD be a repeat of a previous number.

Jacamar answered the question, but not in the most effect way, even though it is the "traditional" way.

My method is the proper way to handle non-repeating numbers.  i.e. pre-assign the numbers, then shuffle.

Mathematically, shuffling results in exactly two cycles: one to load each element of the array, and one to shift each element of the array to a now location.

The traditional method works well at the beginning of the process, but takes longer as you approach the end of the process.  Each time through the loop, it has to check each item in the array to see if there is a match, then restart if it finds one.  For a small sample out of a large range, this is relatively fast, but for a large sample, this will have a lot of restarts.
0

LVL 22

Expert Comment

ID: 8962196
(Repost/update)
DeAn's comment does NOT answer the question.  It offers a random number which COULD be a repeat of a previous number.

Jacamar answered the question, but not in the most effective way, even though it is the "traditional" way.  Later, DocM offered a similar solution.

My method is the proper way to handle non-repeating numbers.  i.e. pre-assign the numbers, then shuffle.
Rhaedes offered a solution similar to mine, but with static starting values.  this would be problematic if the list exceeded nine since his assumption was character-based rather than number based (so "10" would be treated as a 1 and a 0...again!)

Mathematically, shuffling results in exactly two cycles: one to load each element of the array, and one to shift each element of the array to a now location.

The traditional method works well at the beginning of the process, but takes longer as you approach the end of the process.  Each time through the loop, it has to check each item in the array to see if there is a match, then restart if it finds one.  For a small sample out of a large range, this is relatively fast, but for a large sample, this will have a lot of restarts.
0

LVL 6

Expert Comment

ID: 8962406
Apologies - there was an error in the generation of the message recommendation here.

The post that I HAD INTENDED WAS IN FACT

rspahitz

and not DeAn's, and my follow-up description explaining the choice was intended for the 52-card shuffling algorithm, which can be 'easily adapted as desired', and is 'with comments'

I will without haste correct the cleanup post.
0

## Featured Post

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code thatâ€¦
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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â€¦
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process fromâ€¦
###### Suggested Courses
Course of the Month11 days, 1 hour left to enroll