Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

# Random combinations of a group of numbers

Posted on 2003-11-22
Medium Priority
481 Views
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
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
• 6
• 3
• 3
• +1

LVL 6

Expert Comment

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

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

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

Author Comment

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

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

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

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

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

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

ID: 9804733
brother7,

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

Author Comment

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

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

LVL 1

Expert Comment

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
For i = 0 To List1.ListCount - 1
If List1.List(i) = MyTXT Then
Exit For
End If

Next i
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

ID: 9804930
The First works for numbers from 1

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

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone â€¦
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, 17 hours left to enroll