2 Replies - 1154 Views - Last Post: 20 March 2013 - 06:45 AM Rate Topic: -----

#1 VB_user  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 19-March 13

random numbers

Posted 19 March 2013 - 04:30 AM

Hi amtrying to generate random numbers but in visual basic 2010 and i want to make sure that once a random number has been generated it cannot be generated again. this is because i want the random number to act as a unique identifier
cheers guys
Is This A Good Question/Topic? 0
  • +

Replies To: random numbers

#2 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1533
  • View blog
  • Posts: 4,918
  • Joined: 25-September 09

Re: random numbers

Posted 19 March 2013 - 04:39 AM

create random number
check it against numbers you already have
if found, generate new random number
repeat as needed.
Was This Post Helpful? 0
  • +
  • -

#3 dbasnett  Icon User is offline

  • D.I.C Addict

Reputation: 102
  • View blog
  • Posts: 584
  • Joined: 01-October 08

Re: random numbers

Posted 20 March 2013 - 06:45 AM

Simple example

    Dim prng As New Random
    Dim someNumbers As New List(Of Integer)

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        'set up the possible numbers to pick if needed
        If someNumbers.Count = 0 Then
            someNumbers = Enumerable.Range(1, 10).ToList
        End If
        Debug.WriteLine(pickNum)
    End Sub

    Private Function pickNum() As Integer
        Dim idx As Integer = prng.Next(0, someNumbers.Count)
        Dim retval As Integer = someNumbers(idx)
        someNumbers.RemoveAt(idx)
        Return retval
    End Function


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1