Creating Unique Random Integers in an Array 
Author Message
 Creating Unique Random Integers in an Array

I'm using Visual Basic.net and I need to create an array
with 50 -unique- integers. (not one of them repeat) I have
written the code needed to create the random numbers...but
I'm having a problem with the validation of uniqueness of
each array element. I'm trying to check for uniqueness
against all the elements below the current one and all
elements above, to avoid endless loops, but it's not
working. Maybe I am doing it wrong. I appreciate anyone
who sheds some light on this.


Tue, 05 Jul 2005 02:00:44 GMT  
 Creating Unique Random Integers in an Array
Let's see your code..


Quote:
> I'm using Visual Basic.net and I need to create an array
> with 50 -unique- integers. (not one of them repeat) I have
> written the code needed to create the random numbers...but
> I'm having a problem with the validation of uniqueness of
> each array element. I'm trying to check for uniqueness
> against all the elements below the current one and all
> elements above, to avoid endless loops, but it's not
> working. Maybe I am doing it wrong. I appreciate anyone
> who sheds some light on this.



Tue, 05 Jul 2005 03:27:22 GMT  
 Creating Unique Random Integers in an Array
I hope you already knew how to write this but here it is.

Dim array As Integer() = New Integer(49) {}
        Dim counter As Integer, randomnumber As New Random
        Dim data As String

        For counter = 0 To array.GetUpperBound(0)
            array(counter) = randomnumber.Next(1, 50)
            data &= array(counter) & vbCrLf
        Next

        DataBox.Text = data



Wed, 06 Jul 2005 08:49:04 GMT  
 Creating Unique Random Integers in an Array

Quote:

> I hope you already knew how to write this but here it is.

What the previous poster meant was for you to show us your code that was "not
working".

Quote:
> Dim array As Integer() = New Integer(49) {}
>         Dim counter As Integer, randomnumber As New Random
>         Dim data As String

>         For counter = 0 To array.GetUpperBound(0)
>             array(counter) = randomnumber.Next(1, 50)
>             data &= array(counter) & vbCrLf
>         Next

>         DataBox.Text = data

Nevertheless, there's an important detail you missed from your original post:
You seem to want the numbers 1 to 50 in a random order, not 50 unique numbers
in a random order.

As such I suggest you look at a pack of cards...

BTW What you have here is an algorithm/design issue, not a VB.NET issue.

If you're really stuck, post some code that seems to attempt what you want and
I'm sure someone will help.

Regards,
Mark Hurd, B.Sc.(Ma.) (Hons.)



Wed, 06 Jul 2005 10:49:44 GMT  
 Creating Unique Random Integers in an Array

It seems that what you are looking for is how to permute the numbers from 1
to 50.
Try this modification to your code:

 Dim array As Integer() = New Integer(49) {}
         Dim counter As Integer, randomnumber As New Random
         Dim data As String

        'fill the array with the numbers "in order"
         For counter = 0 To array.GetUpperBound(0)
             array(counter) = counter+1
         Next

         dim ToMove as integer
        dim tmp as integer

         For counter = 0 To array.GetUpperBound(0)
             ' decide which one goes in position "counter" but starting form
the right
             ToMove=randomnumber.Next(1, 50-counter)
            tmp=array(50-counter -1)
            array(50-counter -1)=array(ToMove)
            array(ToMove)=tmp
         Next

        data=""
         For counter = 0 To array.GetUpperBound(0)
               data &= array(counter) & vbCrLf
         Next

         DataBox.Text = data



Thu, 07 Jul 2005 10:58:51 GMT  
 Creating Unique Random Integers in an Array
Almost, but there is no point in restricting the ToMove value, and this
code will not randomise the array(1).

        For counter = 0 To array.GetUpperBound(0)
            ToMove = randomnumber.Next(1, array.GetUpperBound(0))
            tmp = array(counter)
            array(counter) = array(ToMove)
            array(ToMove) = tmp
        Next

If performance is a concern you could also move array.GetUpperBound into a
variable, but for 50 numbers...

Cheers,
  Jason

Quote:

> It seems that what you are looking for is how to permute the numbers from 1
> to 50.
> Try this modification to your code:

>  Dim array As Integer() = New Integer(49) {}
>          Dim counter As Integer, randomnumber As New Random
>          Dim data As String

>         'fill the array with the numbers "in order"
>          For counter = 0 To array.GetUpperBound(0)
>              array(counter) = counter+1
>          Next

>          dim ToMove as integer
>         dim tmp as integer

>          For counter = 0 To array.GetUpperBound(0)
>              ' decide which one goes in position "counter" but starting form
> the right
>              ToMove=randomnumber.Next(1, 50-counter)
>             tmp=array(50-counter -1)
>             array(50-counter -1)=array(ToMove)
>             array(ToMove)=tmp
>          Next

>         data=""
>          For counter = 0 To array.GetUpperBound(0)
>                data &= array(counter) & vbCrLf
>          Next

>          DataBox.Text = data



Thu, 07 Jul 2005 12:17:04 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Creating an array of unique random numbers

2. Generate list of unique random integer values?

3. Generating an array of 50 unique integers

4. Converting integers and long integers to byte arrays

5. How to pass an integer array/userdef type array into an Oracle Stored procedure

6. Unique, random list of values

7. Unique Random Numbers???

8. Generating Unique random numbers in a list box

9. Set of random numbers, unique

10. Random String/Integer Generator

11. Jumble into Random Order the Integers in a Column

12. Generate a random integer between x and y

 

 
Powered by phpBB® Forum Software