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

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
0
Lainey_bloggs
Asked:
Lainey_bloggs
  • 4
  • 3
  • 2
  • +7
1 Solution
 
JacamarCommented:
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
 
JacamarCommented:
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
 
DeAnCommented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
DeAnCommented:
nevermind that, that's not what you asked for :-P
0
 
HigorCommented:
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
 
rspahitzCommented:
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.
0
 
DocMCommented:
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
 
PNJCommented:
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
 
rspahitzCommented:
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
 
RhaedesCommented:
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
 
Yodude565Commented:
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
 
CleanupPingCommented:
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
 
GPrentice00Commented:
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
0
 
GPrentice00Commented:
DeAn's post although apparently used to answer a different question can easily be adapted to do exactly as desired, with comments
0
 
rspahitzCommented:
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
 
rspahitzCommented:
(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
 
GPrentice00Commented:
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

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

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