?
Solved

Random combinations of a group of numbers

Posted on 2003-11-22
14
Medium Priority
?
475 Views
Last Modified: 2010-05-01
Hello,

I need to randomly group the numbers 0 to 90.

example for a group of 1 to 5:

12345
13254
15423
etc.
But I need this for 1 to 90.


Thanks Vbal











0
Comment
Question by:vbal
[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
  • Learn & ask questions
  • 6
  • 3
  • 3
  • +1
14 Comments
 
LVL 6

Expert Comment

by:prasitlee
ID: 9804430
Dear vbal,
      Try the following procedure.

Private Function GetRandomNumber() As Integer
    GetRandomNumber = Round(Rnd() * 89) + 1
End Function

      I am strongly sure that this procedure would give you what you are looking for.
                                                                             Meng
0
 

Author Comment

by:vbal
ID: 9804487
thanks,

But this only gives me a random number from 1 to 90.  I need to group all numbers from 1 to 90 in a diferent random order.

1 to 5 coud have these orders as an example:


12345
14532

etc.

the output will be all numbers from 1 to 90 displayed in a new order every time.
0
 
LVL 6

Expert Comment

by:prasitlee
ID: 9804510
Dear vbal,
     How many random number you need to group all of them together ?
     In the case of 1-5 , you show that you need 5 random numbers to be grouped together.
                                                                     Meng
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:vbal
ID: 9804592
MENG,

For my case I require that all 90 numbers be grouped together.  The 5 is just an example of what I would like for 90 numbers.

0
 
LVL 6

Expert Comment

by:prasitlee
ID: 9804616
Dear vbal,
     Try the following one and give me your comment.
Option Explicit

Private Sub Command1_Click()
    Dim strOutput As String
    Dim iRnd As Integer
    Dim strRnd As String
    Dim i As Integer
   
    strOutput = ""
    For i = 1 To 90
        iRnd = GetRandomNumber
       
        If iRnd < 10 Then
            strRnd = "0" & iRnd
        Else
            strRnd = iRnd
        End If
       
        strOutput = strOutput & strRnd
    Next i
   
    MsgBox strOutput

End Sub
Private Function GetRandomNumber() As Integer
    GetRandomNumber = Round(Rnd() * 89) + 1
End Function

0
 

Author Comment

by:vbal
ID: 9804707
Meng,

No this gives repeat numbers.

Look at it this way if we have the characters A,B,C,D,E
we want to show random orderings of these characters.

Some possible orderings would be:

A,C,B,E,D
or
E,C,D,B,A
or
D,B,C,A,E

I want different combinations of all the integers from 1 to 90.  The combination cannot have repeated integers nor should it be missing any of the integers that exist from 1 to 90.  I just want to randomly order these integers.

Another example for 1 to 5

We have the following integers 1 2 3 4 5
I want to show a new random order of these five integers.

An example of a different order of these five integers could be:

5 4 3 2 1
1 3 2 5 4
etc.

I just want to randomly reorganize the order in which the integers appear.


vbal




0
 
LVL 4

Expert Comment

by:brother7
ID: 9804711
I'm assuming the following:
1- Single digits are represented as single digits, not 0x.
2- All numbers 0-90 are to appear only once.

I'm working in VB.NET.  The following function works.  You'll have to adjust for VB6.  I've included a VB6 random number function for 0-90 inclusive at the end.

--- Code Start ---
   Function GenerateRandomString() As String
      Dim randomObject As Random = New Random()
      Dim randomNumber As Integer
      Dim count As Integer = 0
      Dim output As String
      Dim tally As Boolean() = New Boolean(91) {}

      Do While count < 91
         randomNumber = randomObject.Next(0, 91)

         If tally(randomNumber) = False Then
            output &= randomNumber
            tally(randomNumber) = True
            count += 1
         End If

      Loop

      Console.WriteLine(output)
   End Function
--- Code End ---

In VB6,
   randomNumber = Int(91 * Rnd())
0
 
LVL 4

Expert Comment

by:brother7
ID: 9804718
In your original post, you say you want numbers 0-90.  In a later post, you say you want numbers 1-90.  Please clarify.
0
 
LVL 4

Accepted Solution

by:
brother7 earned 150 total points
ID: 9804725
A couple of corrections to my last post.
- upperbound for a 0-based array should be 90, not 91
- in the VB6 random number generator, don't forget to include Randomize() at start of function
0
 

Author Comment

by:vbal
ID: 9804733
brother7,

your assumptions are correct.  However I am having a hard time coding this in vb6.
0
 

Author Comment

by:vbal
ID: 9804779
My vb6 code:

Function GenerateRandomString() As String
   
      Dim count As Integer
      Dim output As String
      Dim tally(90) As Boolean

      Do While count < 90
         randomNumber = randomNumber = Int(90 * Rnd())


         If tally(randomNumber) = False Then
            output = output & randomNumber
            tally(randomNumber) = True
            count = count + 1
         End If

      Loop

      MsgBox (output)
   End Function

I get subscript out of range at: If tally(randomNumber) = False Then
0
 

Author Comment

by:vbal
ID: 9804789
Thanks, brother7.  Got it working.  Just a typo error on my behalf.
vbal
0
 
LVL 1

Expert Comment

by:anaadoul
ID: 9804899
hi
i think i understood what you want
you need to put a list box Called List1
it can be hidden
and command button called Command1


Private Const FromNumber = 1
Private Const ToNumber = 10

Private Sub Command1_Click()
    MN = 1
    For i = 1 To ToNumber
        MN = MN * i
    Next i
    For n = 1 To MN
ZZ:
    Me.Caption = n
    DoEvents
        Dim Myarr(FromNumber To ToNumber) As Integer
        For i = FromNumber To ToNumber
            Myarr(i) = -1
        Next i
        For i = FromNumber To ToNumber
            Dim RN As Integer
            RN = GetRandomNumber
            Do Until Myarr(RN) = -1
                RN = GetRandomNumber
            Loop
            Myarr(RN) = i
           
        Next i
        MyTXT = ""
        For i = FromNumber To ToNumber
           
            MyTXT = MyTXT & Myarr(i)
        Next i
        addit = True
        For i = 0 To List1.ListCount - 1
            If List1.List(i) = MyTXT Then
                addit = False
                Exit For
            End If
           
        Next i
        If addit Then List1.AddItem MyTXT Else GoTo ZZ
    Next n
End Sub

Private Function GetRandomNumber() As Integer

    GetRandomNumber = Int((ToNumber * Rnd) + 1)

End Function




hope it works well
0
 
LVL 1

Expert Comment

by:anaadoul
ID: 9804930
OOPS Please EDIT
The First works for numbers from 1
to add the ZERO


Private Sub Command1_Click()
    MN = 1
    If FromNumber = 0 Then XN = ToNumber + 1 Else XN = ToNumber
.
.
.
.
Private Function GetRandomNumber() As Integer

    GetRandomNumber = Int((ToNumber - FromNumber + 1) * Rnd + FromNumber)

End Function
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…
Suggested Courses
Course of the Month11 days, 18 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question