Random Numbers again!!!

Hi there,

i think i have gotten into a bit of a fix...

here is the problem: i have used the random obj to generate random numbers between 1 to 90. some times when the application runs, it picks a number twice.

for example an output would be 2,8,34,2,67....

is it possible to make sure all numbers picked are only selected once

that is if i request for 10 ranadom numbers, each 10 digits would be unique... this is driving me nutts!!

thanks 4 ur help

Slow.



Answer this question

Random Numbers again!!!

  • Benjamin Noyce

    Thanks !

    You just made my day !



  • PitBull.ja

     

    Hopefully this will work for you:

    Dim r As New Random

    Dim n As Integer

    Dim i As Integer

    Dim a As New List(Of Integer)

    For i = 1 To 10

      ' Get a random number

      n = r.Next(1, 90)

      ' Make sure it's not already in the list

      Do While a.Contains(n)

        n = r.Next(1, 90)

      Loop

      ' add it to our list

      a.Add(n)

    Next

    This works by getting a random number in a given range, looking to see if it's in the list of previous numbers, if it's not it adds it, otherwise it gets another random number and looks again.

    Alternatively, if you want to replace the List with a listBox that would work too: use listBox1.Items.Contains instead.

    A word of warning though - if you narrow the random number range down to a small subset, e.g. 1,10 instead of 1,90, this routine may take some time to run.

    HTH



  • James-Jones

    PsychUK

    VB6 or not, I like the elegance of your solution.



  • JodyByrd

    thanks all you've been a great help!!

    Great Great help!!


  • ovatsus

    'I've not actually tried any of the coding '

    Perhaps, as Renee suggests, I did not post my question in the right thread; but my original reason for posting was that I don't find HTS's code snippet works - but it looks right.

    My own code example works on my machine but it looks wrong (to me). I was hoping someone with more experience might try both.

    Mongkut


  • Mando

    I want a fixed range of numbers in a random order for two reasons:

    1. 1-26 gives me the alphabet

    2. I want to pull questions out of an array in a random order

    I don't understand your comment >You didn't really want random numbers to begin with<

    are you making a distinction between a set of randomised numbers and a randomised set of numbers

    Mongkut


  • Amr Ashraf

    Mongkut....

    Could I ask a question

    Do you just not want consecutive repeats or no repeats.... which is difficult if there are only 26 possibilities.

    I fear the way you asked you question, or specified what your wanted (spec'ed) was ambiguous.



  • DaFeiFan

    Another option may be to create an array of the numbers 1 to 10 (or 1 to 100, etc.) in order, then do a random 'sort' on them by picking 2 at random and swapping them. do that 100 times or so.

    Then as you request random numbers go through the list one by one.



  • jorrit5477

    Hi Slow,

    One way would be to keep a list of previously generated numbers, whenever a number is generated check the list, if the number exists in the list generate another random number, check the list, and so on...



  • LPATTERSON

    A property of randomly generated number is that at times there will be two consecutive numbers.

    You didn't really want random numbers to begin with.



  • DrEvil

    I've not actually tried any of the coding but in the old days you had to pick random numbers using the Rnd() function (still supported) this gives you a random decimal number always less that 1 (ie. 0.646432) and can return 0. You simply multiply by the number of possablities and integer the number so Int(Rnd()*10) would return a random number between 0 and 9.

    So to get a random uppercase letter:

    Chr(Int(Rnd()*26)+65)

    and a random lowercase letter:

    Chr(Int(Rnd()*26)+97)

    I think I would prefer this method, but that's because I'm still thinking like a VB6 programmer. Do it whichever way you think is best.



  • LISABET

    A set of randomly occurring number will have consecutive repeating numbers.



  • ADG

    He wants the numbers 1-26 in a random order. The Original Poster wanted 10 numbers from a pool without repeats.

    anyhow:

    "What puzzles me is why

    n = r.Next(0, 26) + 1

    appears to return a number in the range 1 to 26 and not 1 to 27. "


    Well in that situation r.Next will return integers from 0 up to, but not including 26. see the msdn library for Rand.Next(Int32, Int32). So you end up with 1 to 26.

    Your code works...


    I like the random sorter technique:

    Public Class Form1
     
        Sub New()
           InitializeComponent()
           Dim numbers(25) As Integer
           For index As Integer = 0 To 25
               numbers(index) = index + 1
           Next
           ' radnomly sort the array (we'll do it a few times)
           Dim sorter As New RandomSorter
           For count As Integer = 0 To 6
               Array.Sort(numbers, sorter)
           Next
           ' array is now randomly sorted
           For index As Integer = 0 To 25
               Console.WriteLine(numbers(index))
           Next
     
        End Sub
    End Class
     
    Public Class RandomSorter
        ' nomally you implement this to Sort an Array. We use it to UnSort the array. 
        Implements IComparer(Of Integer)
     
        Private rand As Random
     
        Sub New()
           rand = New Random
        End Sub
     
     
        Public Function Compare(ByVal x As Integer, ByVal y As Integer) As Integer Implements System.Collections.Generic.IComparer(Of Integer).Compare
           ' must return 0 if they are the same 
           If x = y Then Return 0
           ' otherwise -1 or 1 depending on wether x should come before y - we decide at random
           Dim i As Integer = rand.Next(0, 2) ' this generates 0 or 1, 2 is never reached
           If i = 0 Then Return -1
           Return 1
        End Function
     
    End Class


    The sensible way is to have a list of the numbers, draw one at random and then remove it from the list so that it isn't drawn in the next loop:

           Dim numberPool As New List(Of Integer)
           For number As Integer = 1 To 26
               numberPool.Add(number)
           Next
           Dim rand As New Random
           For i As Integer = 0 To 25
               ' draw a number from the pool
               Dim draw As Integer = numberPool.Item(rand.Next(0, numberPool.Count))
               Me.ListBox1.Items.Add(draw)
               ' remove it from the pool
               numberPool.Remove(draw)
           Next




  • MariusVE

    HTH

    In your post about random numbers, you said 'hopefully it will work' but I wanted a random list of numbers in a range.

    The following appears to work. The form has a listbox only.

    Public Class Form1

    Dim r As New Random

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    'load the numbers 1 to 26 into a list box

    Dim n As Integer = 0

    Dim i As Integer = 0

    For i = 0 To 25

    n = r.Next(0, 26) + 1

    Do While ListBox1.Items.Contains(n)

    n = r.Next(0, 26) + 1

    Loop

    ListBox1.Items.Add(n)

    Next i

    End Sub

    End Class

    --------------------------

    I've tested this several times and it delivers the goods. I've also tried it in a sorted list box so it's easier to see.

    What puzzles me is why

    n = r.Next(0, 26) + 1

    appears to return a number in the range 1 to 26 and not 1 to 27. Am I wrong

    Mongkut


  • Random Numbers again!!!