Link to home
Start Free TrialLog in
Avatar of Lainey_bloggs
Lainey_bloggs

asked on

non repeating random numbers

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
Avatar of Jacamar
Jacamar

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
         
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
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
nevermind that, that's not what you asked for :-P
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

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
   intCard(intCardCntr) = intCardCntr ' start with the deck in sequence
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.
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

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... :-) )
ASKER CERTIFIED SOLUTION
Avatar of rspahitz
rspahitz
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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.
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.
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:

-->Accept DeAn's comments as answer

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

GPrentice00
EE Cleanup Volunteer
DeAn's post although apparently used to answer a different question can easily be adapted to do exactly as desired, with comments
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.
(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.
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.