none
Random Number Generator in VB

    Question

  • Hi,

    I am new to Visual studio 2008 using the express version

    I wish to generate up to 10 000 random numbers without duplicates

    Can anybody assist with the VB code and an appropriate container to store and print these nummbers

    Many thanks
    Friday, September 11, 2009 2:35 PM

Answers

  •   Dim TotalPopulation As Integer = 10000
      Dim SamplePopulation As Integer = 2300
      Dim SamplePopulationList As New List(Of Integer)
      Dim Seed As Integer
      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Seed = Convert.ToInt32(Now.Ticks Mod Integer.MaxValue) 
        Dim Rndm As New Random(Seed)
        Dim TotalPopulationList As New List(Of Integer)
        For I As Integer = 0 To TotalPopulation - 1
          TotalPopulationList.Add(I)
        Next
        For I As Integer = 0 To SamplePopulation - 1
          Dim J As Integer = Rndm.Next(TotalPopulationList.Count)
          SamplePopulationList.Add(J)
          TotalPopulationList.RemoveAt(J)
        Next
      End Sub
    
    
    • Edited by JohnWein Friday, September 11, 2009 7:54 PM
    • Marked as answer by Seon Monday, September 14, 2009 8:26 PM
    Friday, September 11, 2009 6:27 PM

All replies

  •  

    Check this link for distinct Random Numbers..without duplicates:
    http://vbnet.mvps.org/index.html?code/helpers/randomunique.htm

    Thanks,
    Veera Reddy
    Friday, September 11, 2009 2:49 PM
  • here is some code. you can change the number of numbers in the list to however many you want


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            'list to store numbers
            Dim numbers As New List(Of Integer)
    
            'add desired numbers to list
            For count As Integer = 1 To 10
                numbers.Add(count)
            Next
    
    
            Dim Rnd As New Random
            Dim SB As New System.Text.StringBuilder
            Dim Temp As Integer
    
            'select a random number from the list, add to listbox and remove it so it can't be selected again
            For count As Integer = 0 To numbers.Count - 1
                Temp = Rnd.Next(0, numbers.Count)
                SB.Append(numbers(Temp) & "  ")
                ListBox1.Items.Add(numbers(Temp))
                numbers.RemoveAt(Temp)
            Next
    
            'display numbers
            Label1.Text = SB.ToString
        End Sub
    • Proposed as answer by jwavila Friday, September 11, 2009 5:39 PM
    Friday, September 11, 2009 2:51 PM
  • private Sub GenerateRN()
    Dim random As New Random()
            Dim arr(10000) As Integer
            For i As Integer = 0 To 10000
                arr(i) = random.Next(0, 2000000)
            Next
    End Sub

    • Edited by AUmidh Friday, September 11, 2009 2:55 PM
    Friday, September 11, 2009 2:52 PM
  • As printing is concern it depends on type of application if you use console base application then use Console.Write or Console.WriteLine() and if you are using Windows application then display it into list box or text box.
    inform me about type of application i will let you with code.
    Friday, September 11, 2009 2:54 PM
  • Hi Veera,

    Many thanks for you assistance, i will try your coding

    sorry about the vote it should be 10, but I am unble to vote again

    Many thanks

    Seon
    • Marked as answer by Seon Friday, September 11, 2009 3:24 PM
    • Unmarked as answer by Seon Friday, September 11, 2009 3:24 PM
    Friday, September 11, 2009 3:16 PM
  • Hi Aumidh,

    Many thanks for your reply

    The type of application is a windoes forms application and I am going to try Veera's code
     
    Manty thanks

    Seon
    Friday, September 11, 2009 3:20 PM
  • Hi Jwavila,

    Many thanks for your assistance

    Seon
    Friday, September 11, 2009 3:32 PM
  • The only statistically valid code in this thread is that posted by jwavila.  Aumidh's code doesn't prevent duplicates and Veera's code is flawed statistically.  Adapt jwavila's code to VB6, which you seem to want.  Posting on a VB6 forum should provide more appropriate answers.
    • Marked as answer by Seon Friday, September 11, 2009 4:34 PM
    • Unmarked as answer by Seon Friday, September 11, 2009 6:05 PM
    Friday, September 11, 2009 3:32 PM
  • Hi AUmidh,

    Many thanks

    I am using windows form application

    Your code looks great
     So the printing code would be aprciated

    Many thanks

    Seon
    Friday, September 11, 2009 3:46 PM
  • if you want duplicates then there is no problem with aumidh's code
    Asgar
    Friday, September 11, 2009 4:24 PM
  • @seon
    Add a listbox into your form so that the code will print/add/display the numbers in the listbox. or tell me in which control you want to display the numbers.
    private Sub GenerateRN()
    Dim random As New Random()
            Dim arr(10000) As Integer
            For i As Integer = 0 To 10000
                arr(i) = random.Next(0, 2000000)
                listbox1.Items.Add(arr(i).ToString());
            Next
    End Sub
    
    Friday, September 11, 2009 4:27 PM
  • Hi JohnWein,

    Thanks for your advice

    I do wish to use the random numbers for statistical sampling and therefore need statistically valid random numbers

    I abandoned VB6 in about 2003 due to the NET concept

    It is really disappointing that Visual Studio 2008 cannot give me a statistically valid range of random numbers ???????? :(    

    Aumidh's code  does give duplicate numbers

    It would be appreciated if you could give memore guidance as you have an insight into what I require

    I have already written code for Confidence Level, Confidence Interval and Samplesize but am now having problems generating random numbers for sample selection

    your assistance would really be appreciated

    Seon



    • Edited by Seon Friday, September 11, 2009 4:43 PM
    Friday, September 11, 2009 4:28 PM
  • @Seon
    If you don't want duplicate entry in your storage then i will send you that code.
    Friday, September 11, 2009 4:33 PM
  • Hi Jwavila,

    It seems you are the "star of the day"

    Thank you, much appreciated

    Seon
    Friday, September 11, 2009 4:39 PM
  • Check the complete version without duplicates
     Private arr(10000) As Integer
        Private index As Integer = 0
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim random As New Random()
            For i As Integer = 0 To 10000
                Dim num As Integer = random.Next(0, 2000000)
                If IsExist(num) Then
                    i = i - 1
                    Continue For
                End If
                index = i
                arr(i) = num
            Next
        End Sub
        Private Function IsExist(ByVal num As Integer) As Boolean
            For i As Integer = 0 To index
                If arr(i) = num Then
                    Return True
                End If
            Next
            Return False
        End Function
    Friday, September 11, 2009 4:40 PM
  • Check the complete version without duplicates
     Private arr(10000) As Integer
    
        Private index As Integer = 0
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim random As New Random()
    
            For i As Integer = 0 To 10000
    
                Dim num As Integer = random.Next(0, 2000000)
    
                If IsExist(num) Then
    
                    i = i - 1
    
                    Continue For
    
                End If
    
                index = i
    
                arr(i) = num
    
            Next
    
        End Sub
    
        Private Function IsExist(ByVal num As Integer) As Boolean
    
            For i As Integer = 0 To index
    
                If arr(i) = num Then
    
                    Return True
    
                End If
    
            Next
    
            Return False
    
        End Function
    
    

    This conforms to no valid statistical model.  It's selection with replacement, but don't allow duplicates.  The valid statistical models can be shuffle and select one at a time or select without replacement.  Wikipedia is an excellent reference for mathematical modeling.
    Friday, September 11, 2009 4:54 PM
  • Hi AUmidh,

    Many thanks again

    Seon
    Friday, September 11, 2009 5:36 PM
  • Hi JohnWein,

    I believe I have the correct statistical model but will look at Wikipedia, thank you

    I possibly was too vague in my initial enquiry

    This is what I require:

    Given a total poulation of say 10 000 items or integers I have already written the code to calculate the sample size say 2300 items

    What i now require is to select a random sample of 2 300 integers from the total poulation of 10 000 without replacement

    I would really be appreciative if you are able to assist

    And thank you for you input

    Seon
    Friday, September 11, 2009 5:50 PM
  • Hi AUmidh,

    the code to print a listbox and label would be appreciated

    Many thanks

    Seon
    Friday, September 11, 2009 5:57 PM
  •   Dim TotalPopulation As Integer = 10000
      Dim SamplePopulation As Integer = 2300
      Dim SamplePopulationList As New List(Of Integer)
      Dim Seed As Integer
      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Seed = Convert.ToInt32(Now.Ticks Mod Integer.MaxValue) 
        Dim Rndm As New Random(Seed)
        Dim TotalPopulationList As New List(Of Integer)
        For I As Integer = 0 To TotalPopulation - 1
          TotalPopulationList.Add(I)
        Next
        For I As Integer = 0 To SamplePopulation - 1
          Dim J As Integer = Rndm.Next(TotalPopulationList.Count)
          SamplePopulationList.Add(J)
          TotalPopulationList.RemoveAt(J)
        Next
      End Sub
    
    
    • Edited by JohnWein Friday, September 11, 2009 7:54 PM
    • Marked as answer by Seon Monday, September 14, 2009 8:26 PM
    Friday, September 11, 2009 6:27 PM
  • Hi JohnWein,

    Many thanks for your trouble

    When I run the appliction it gives an overflow exception at Seed = Convert.ToInt32(Now.Ticks)

    Please do not laugh at me, but the sample, I am try to ascertain is that = SamplePopulationList ?

    Or How do I reflect the sample in a Listbox or label

    Thank you so much for your effort and assistance, it is really appreciated

    Seon 

    Friday, September 11, 2009 7:18 PM
  • Change it to seed = Convert.ToInt32(Now.Ticks Mod Integer.MaxValue)

    I'll edit the code.
    Friday, September 11, 2009 7:51 PM
  • John

    after the OP posted "you are the star of the day"  (we'll ignore that fallacy for now), I thought the thread was answered (this is why I rarely mark my own as proposed answer - unless they come back with a comment like that).

    and I figured it was enough to get the OP towards what was wanted.

    unfortunately I can't un-mark as proposed as answer. My apologies for jumping the gun - wasn't trying to ignore the excellent help you are now providing.

    Joe
    Friday, September 11, 2009 9:32 PM
  • Joe,

    I must apologise if I have caused you any embarrasment, I am new to MSDN and Visual Studio 2008 and was so greatful for the help you had so enthusiastically offered that I incorrectly marked the topic as "answered"

    So the culpability for any misunderstanding lies with me

    Thank you for your input and the very good solution you offered 

    It was John's comment "The only statistically valid code in this thread is that posted by jwavila" and the fact that John suggested I use your code that prompted me to make the comment I did

    I also did not realise that the random numbers generated were with replacement

    It was almost as though John could read my mind

    I extend both my thanks for your assistance and my apologies for any embarassment

    John, thank you for your tireless effort in assisting me, I am very greatful

    Seon
    Friday, September 11, 2009 10:17 PM
  • John,

    I find your insight, knowldge and programming ability exceptional and brilliant

    I was reasonable proficient in VB6, nothing like you or Joe

    I was badly advised the Visual Basic Users Group of South Africa, the Beta for Vb NET had just been released and the advice given me was go VB6 rather that net.

    I incurrened considerable cost and time learning and studying VB6 and after 3 years of effort abandoned VB6, i was reasonably proficient

    I find there is such a paradigm shift with VB NET and am struggeling. I am determined to be reasonably proficient with VB NET

    I will be 63 next month and plan to spend my retirement doing development but as must be evident to you I have a long, long road to travel

    I find you to be extremely brilliant and knowledgable and will never attain you level of proficiency but am determined to be at least mediocre developer

    I am very greatful for your assistance and admire you knowledge and skill

    Should I have caused any tension between you and Joe please accept my apology

    It is nearly 1:ooam here, I will spend the weekend studying the code you have so kindly sent me and hope to receive the amended code next week

    Have a great weekend

    thank you again

    Seon
    Friday, September 11, 2009 10:57 PM
  • Seon

    no need to apologize for anything at all.

    and you have not caused any tension between John and I. I just noticed that John was helping you further with the code I provided, and didn't want him or anyone else to feel that my post was "the answer". I just felt it was presumptous of me to mark my post as the proposed answer, and was apologizing for such.

    I respect John's in-depth knowledge, and we are fortunate to have him helping on this forum.

    It had nothing to do with you. Glad you posted that question, and welcome to the Forum. I hope you enjoy playing with VB.NET. I know I enjoy it - and I am not a "professional" or anything like that.

    Joe
    Friday, September 11, 2009 11:27 PM
  • Joe

    It is good to know I have not caused any tension

    Thank you for the welcome

    I am passionate about VB.NET

    And I note you are a 4 star General and you seem like a professional to me

    Thank you again

    Have a great weekend

    Seon
    Friday, September 11, 2009 11:35 PM
  • Hi John,

    Many thanks for your assistance and patients with me

    I really think you are brilliant

    Kind regards

    Seon
    Monday, September 14, 2009 8:29 PM
  • No need to grovel.  I'm glad I could help a younger forum member.
    Monday, September 14, 2009 8:41 PM
  • Many thanks
    Tuesday, September 15, 2009 11:03 PM