locked
random problem.... RRS feed

  • Question

  •  

    i'm trying pick the 1 number from my array to the listbox but this code pick 2 number
    .where is my mistake.please help somebody.............

    Private
    Sub Bttn1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttn1.Click

     

    Dim number() As String = {0, 2, 8, 5, 5, 7, 6, 6, 1, 2, 5, 6, 9, 9, 1, 5, 8, 8, 3, 3, 3, 3, 7, 5, 1, 7, 6, 3, 7, 5, 5, 5, 0, 1, 1, 3, 4, 1, 2, 4, 1, 1, 9, 8, 8, 9, 8, 2, 1, 7, 0, 8, 4, 5, 5, 6, 7, 4, 0, 6, 3, 9, 1, 2, 5, 0, 1, 8, 5, 1, 0, 8, 7, 8, 9, 2, 6, 8, 1, 9, 1, 8, 6, 5, 5, 2, 4, 1, 4, 5, 4, 8}

     

    Dim rnd As New Random()

     

     

    Randomize()

     

    Dim x As Integer = rnd.Next(0, number.Length - 1)

    lstbox.Items.Add(x)

     

    End Sub

    Sunday, June 28, 2009 11:29 AM

Answers

  • Try This way

     Dim number() As Integer = {0, 2, 8, 5, 5, 7, 6, 6, 1, 2, 5, 6, 9, 9, 1, 5, 8, 8, 3, 3, 3, 3, 7, 5, 1, 7, 6, 3, 7, 5, 5, 5, 0, 1, 1, 3, 4, 1, 2, 4, 1, 1, 9, 8, 8, 9, 8, 2, 1, 7, 0, 8, 4, 5, 5, 6, 7, 4, 0, 6, 3, 9, 1, 2, 5, 0, 1, 8, 5, 1, 0, 8, 7, 8, 9, 2, 6, 8, 1, 9, 1, 8, 6, 5, 5, 2, 4, 1, 4, 5, 4, 8}
            Dim rnd As New Random()
            Randomize()
            Dim x As Integer = rnd.Next(0, number.Length - 1)
            lstBox.Items.Add(number(x).ToString)

    Arjun Paudel
    Sunday, June 28, 2009 1:11 PM
  • Because, it was actually pooling the random numbers from a range of numbers: 0 - 9. Meaning that you can do away with the numbers in your array and it will still work.

    0 is the least random number that you want to return while 9 is the maximum. The +1 is to enable it return 9 which is the highest number that you want to return.

    You can even change it to:

    Dim number() As Integer = {}
    
            Dim rnd As New Random()
    
            Randomize()
    
            Dim x As Integer = rnd.Next(0, 10)
    
            lstBox.Items.Add(x)

    Only performance counts!
    Sunday, June 28, 2009 3:17 PM
  • YiChun I don't think you use Randomize() with the random class do you ? Isn't that the old way before the random class such as this

    ' Initialize the random-number generator.
    Randomize()
    ' Generate random value between 1 and 6.
    Dim value As Integer = CInt(Int((6 * Rnd()) + 1))
    

    That is part of an example from the randomize function in the Microsoft.VisualBasic namespace . This is an example from the random class System.Random

    ' Instantiate random number generator using system-supplied value as seed.
    Dim rand As New Random()
    ' Generate and display 5 random byte (integer) values.
    Dim bytes(4) As Byte
    rand.NextBytes(bytes)
    Console.WriteLine("Five random byte values:")
    

    Coding for fun Be a good forum member mark posts that contain the answers to your questions or those that are helpful
    Monday, July 6, 2009 4:04 AM

All replies


  • Dim x As Integer = rnd.Next(0, maxValue:=9 + 1)
    Change the above line and re-try.

    By the way, I set Option Strict On. For that reason I changed your Array Declaration line to:

    Dim number As Integer instead of Dim number As String. I suugest that you set Option Strict On as well.


    Only performance counts!
    Sunday, June 28, 2009 12:05 PM
  • thank sylva......
    but...
    are sure, ur code pulling number from my array..
    maxValue:=9 + 1


    i delete my array number and run it.it working.....why

    perhaps ur answer sylva
    Sunday, June 28, 2009 12:59 PM
  • Try This way

     Dim number() As Integer = {0, 2, 8, 5, 5, 7, 6, 6, 1, 2, 5, 6, 9, 9, 1, 5, 8, 8, 3, 3, 3, 3, 7, 5, 1, 7, 6, 3, 7, 5, 5, 5, 0, 1, 1, 3, 4, 1, 2, 4, 1, 1, 9, 8, 8, 9, 8, 2, 1, 7, 0, 8, 4, 5, 5, 6, 7, 4, 0, 6, 3, 9, 1, 2, 5, 0, 1, 8, 5, 1, 0, 8, 7, 8, 9, 2, 6, 8, 1, 9, 1, 8, 6, 5, 5, 2, 4, 1, 4, 5, 4, 8}
            Dim rnd As New Random()
            Randomize()
            Dim x As Integer = rnd.Next(0, number.Length - 1)
            lstBox.Items.Add(number(x).ToString)

    Arjun Paudel
    Sunday, June 28, 2009 1:11 PM
  • Because, it was actually pooling the random numbers from a range of numbers: 0 - 9. Meaning that you can do away with the numbers in your array and it will still work.

    0 is the least random number that you want to return while 9 is the maximum. The +1 is to enable it return 9 which is the highest number that you want to return.

    You can even change it to:

    Dim number() As Integer = {}
    
            Dim rnd As New Random()
    
            Randomize()
    
            Dim x As Integer = rnd.Next(0, 10)
    
            lstBox.Items.Add(x)

    Only performance counts!
    Sunday, June 28, 2009 3:17 PM
  • thank a lot Arjun Paudel ur code successful.
    good coding..........from u

    i need ur opnion...............
    when we compile that code.
    everytime start the program and consider the first number is 3
    we close that program
    we open that program again
    why we can't get first number is 3 ???????????????

    u have a code for that? or any suggestion
    thank again

    Sunday, June 28, 2009 4:00 PM
  • thank sylva..
    Sunday, June 28, 2009 4:01 PM
  • Hi Mipakteh,

    i need ur opnion...............
    when we compile that code.
    everytime start the program and consider the first number is 3
    we close that program
    we open that program again
    why we can't get first number is 3 ???????????????
    Random Class represents a pseudo-random number generator, a device that produces a sequence of numbers that meet certain statistical requirements for randomness . Thus, you may not get the same number every time you start the program.
    For more information on Random class, see: http://msdn.microsoft.com/en-us/library/system.random.aspx

    Could you please clarify your requirement more?

    Thanks
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Edited by YiChun Chen Monday, July 6, 2009 2:21 AM typo
    Wednesday, July 1, 2009 8:38 AM
  • yes Yichun Chen as ur link ..it seem like that what i mean..
    but all the code i can't understand yet

    can u help the easy way to understand that...
    not need long ...just to example
    Friday, July 3, 2009 11:04 AM
  • Hi Mipakteh,

    For your better understanding on Random Class, I use Arjun's code to explain the code one by one.

    1. Generate the integer array with 33 values and Random objects

    Dim number() As Integer = {0, 2, 8, 5, 5, 7, 6, 6, 1, 2, 5, 6, 9, 9, 1, 5, 8, 8, 3, 3, 3, 3, 7, 5, 1, 7, 6, 3, 7, 5, 5, 5, 8}
    
    Dim rnd As New Random()

    2. Initialize the random-number generator.

    Randomize()

    For better understanding on Randomize Function, see: http://msdn.microsoft.com/en-us/library/8zedbtdt.aspx

    3. Return a random number within integer array

    Dim x As Integer = rnd.Next(0, number.Length - 1)

    For instance, rnd.Next(0, number.Length - 1) may return 1 or 2.
    Since it is random , you will not know which number will be return within 33 values.

    For more information on Random.Next method, see: http://msdn.microsoft.com/en-us/library/2dx6wyd4.aspx

    Hope this can help you a lot.

    Thanks

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Edited by YiChun Chen Monday, July 6, 2009 3:51 AM typo
    Monday, July 6, 2009 3:50 AM
  • YiChun I don't think you use Randomize() with the random class do you ? Isn't that the old way before the random class such as this

    ' Initialize the random-number generator.
    Randomize()
    ' Generate random value between 1 and 6.
    Dim value As Integer = CInt(Int((6 * Rnd()) + 1))
    

    That is part of an example from the randomize function in the Microsoft.VisualBasic namespace . This is an example from the random class System.Random

    ' Instantiate random number generator using system-supplied value as seed.
    Dim rand As New Random()
    ' Generate and display 5 random byte (integer) values.
    Dim bytes(4) As Byte
    rand.NextBytes(bytes)
    Console.WriteLine("Five random byte values:")
    

    Coding for fun Be a good forum member mark posts that contain the answers to your questions or those that are helpful
    Monday, July 6, 2009 4:04 AM
  • Hi Bdbodger,

    Thank you for your clarification. :)

    I just explained the code that Arjun posted above. You're right. I really appreciate it.

    Thanks

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Monday, July 6, 2009 6:49 AM