Link to home
Start Free TrialLog in
Avatar of teamdad
teamdad

asked on

Working with random numbers

This may be a bit tricky; I’ll try to explain what I am trying to do and hope that there is a way to do it with some code I can use in VB.NET 2002.  For this part of my program I need to take a number of known numbers then mix them with random numbers in a range to make a set of numbers in a specific format.  

The number of known numbers is unknown: Example…there may be 1 known number, 3 known numbers or 10 known numbers…. I think something like having some type of input to add the known numbers to an array and then randomly pick the rest of the needed numbers from a range that the user sets and then randomly mix them all up and display them in a label or something best explains what it is I am looking for.

3 known numbers, needs to have total of 6 numbers, enter the 3 known numbers and they are added to an array, set the range of numbers 0 to 40 and have the program pick 3 numbers that are then added to the array, the numbers are randomly mixed together and then displayed in double digit format for numbers 0 to 9 so that 1 is 01 etc…. the label display might look like this: 08 – 25 – 14 – 07 – 06 – 34

Anyone have a working example of how this can be done?
Avatar of eozz_2000
eozz_2000

Well... I understand that you want to create random number between 2 numbers given by de user?

MsgBox(rnd.Next(CInt(Me.TextBox1.Text), CInt(Me.TextBox2.Text)).ToString.PadLeft(2, "0"))
Avatar of teamdad

ASKER

Almost.... the user will have some number of numbers they already know and the rest they will need random numbers for.  

Example:

I need the user to enter the numbers they already have (not everyone will have the same amount of numbers) once they enter what they have already I need the program to take a pre defined (by the user) range of numbers that it will pick the rest of the numbers from, it will take what the user entered and what it calculated (randomly on it's own) then randomly mix all the numbers again and display the set of numbers.

Still example:

Say a user knows that the range of numbers is between 0 and 54, they only need to have 6 numbers in all, they already know they have number 4, 20, and 32 but they still need to have the program come up with the other three numbers randomly to make them then have the 6 numbers.  The program will pick the other 3 numbers from the user set range of 0 and 54, the program say picks 18, 50, and 7... now they have 4, 20, 32, 18, 50, 7 as their 6 numbers.  The program then needs to take those numbers and mix their order up and then display the 6 numbers in a format like 18 - 7 - 4 - 20 - 50 - 32 to the user.
Avatar of Mike Tomlinson
The following function getNumbers() should do the trick:

Public Class Form1
    Inherits System.Windows.Forms.Form

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim knownValues() As Integer = {4, 20, 32}
        Label1.Text = getNumbers(knownValues, 6, 0, 54)
    End Sub

    Private Function getNumbers(ByVal known() As Integer, ByVal requiredNumber As Integer, _
            ByVal minRange As Integer, ByVal maxRange As Integer) As String

        Dim output As String
        Dim i As Integer
        Dim tmp As Integer
        Dim rndIndex As Integer
        Dim tmpArray(requiredNumber - 1) As Integer
        Dim r As New Random
        Dim randomPool As New ArrayList

        ' build the random pool
        For i = minRange To maxRange
            ' don't include any of the known values
            If Array.IndexOf(known, i) = -1 Then
                randomPool.Add(i)
            End If
        Next

        ' shuffle the random values
        For i = 0 To (randomPool.Count - 1)
            rndIndex = r.Next(0, randomPool.Count)
            tmp = randomPool(i)
            randomPool(i) = randomPool(rndIndex)
            randomPool(rndIndex) = tmp
        Next

        ' populate tmpArray with the known values
        ' and fill in any remaining slots
        ' with values from the shuffled randomPool
        For i = 0 To (requiredNumber - 1)
            If i <= known.GetUpperBound(0) Then
                tmpArray(i) = known(i)
            Else
                tmpArray(i) = randomPool.Item(0)
                randomPool.RemoveAt(0)
            End If
        Next

        ' shuffle the set by swapping
        ' each position with another
        For i = 0 To tmpArray.GetUpperBound(0)
            rndIndex = r.Next(0, tmpArray.GetUpperBound(0))
            tmp = tmpArray(i)
            tmpArray(i) = tmpArray(rndIndex)
            tmpArray(rndIndex) = tmp
        Next

        ' build the output string from
        ' the shuffled set
        For i = 0 To tmpArray.GetUpperBound(0)
            If output = "" Then
                output = Format(tmpArray(i), "00")
            Else
                output = output & " - " & Format(tmpArray(i), "00")
            End If
        Next
        Return output
    End Function

End Class
Avatar of teamdad

ASKER

That got me thinking...... I know how to better explain now.  The user is given a piece of paper that has some numbers on it.... it may have just one number or any amount of numbers on it, the paper also has a range of numbers on it that could be anything and it has how many numbers you need in total, the user don't know what any of these are till they get their piece of paper. They need to enter the range of numbers that is on the paper with two NumericUpDown controls and they have to enter the number of numbers total they need with a textbox and they need to enter the numbers one at a time with another textbox to add to the pool and then push a button.  The program will know how many numbers they need total and the range of numbers it needs to pick the rest from to make the total amount of numbers they entered, it takes what they have entered plus what it picks from the pool and combines them in a random order and displays them on a label.  

It's kinda like you know some of the numbers you want/need but you want the rest of the unknown numbers to be randomly picked from a pool or range of numbers then have them mixed up and displayed so they aren't in any order from the ones you knew and the ones the program picked for you.
Hi,

hope i'm not asking the obvious but... how many numbers are ever needed in total? e.g. you mentioned that with three known numbers that another three are needed for a total of six, how many for n known numbers?

Cheers

BC
Hi teamdad, the algorithm I posted will work for your requirements.  Do you need help putting an interface on it?

~IM
Avatar of teamdad

ASKER

Babycorn-Starfish, it's another tricky part.  The most would be 6 total but the user may have one of the six, two of the six, three of the six, four of the six or five of the six numbers.

Idle Mind, in your code I saw that it hard coded the numbers and the known numbers will not be the same every time.  Another working example would be great if it will still work.
The numbers were hard coded into an array only for the example.

The getNumbers() function itself receives an array of integers so it is flexible.  Just build the array of values as the user enters them and pass them into the function along with the other parameters.

Do I need to build an entire GUI to show how to use it?

~IM
Avatar of teamdad

ASKER

If you wouldn't mind, I would like an example to see how it works.  I only put a label and a button on the GUI I made for it.
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
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