Solved

Random combinations of a group of numbers

Posted on 2003-11-22
14
468 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
  • 6
  • 3
  • 3
  • +1
14 Comments
 
LVL 6

Expert Comment

by:prasitlee
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:vbal
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 4

Expert Comment

by:brother7
Comment Utility
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 50 total points
Comment Utility
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
Comment Utility
brother7,

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

Author Comment

by:vbal
Comment Utility
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
Comment Utility
Thanks, brother7.  Got it working.  Just a typo error on my behalf.
vbal
0
 
LVL 1

Expert Comment

by:anaadoul
Comment Utility
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
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

728 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now