CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    Join Date
    Feb 2004
    Posts
    218

    how to generate a random number

    Hi, everyone,
    How to generate a random number from a given list? Thank you

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: how to generate a random number

    Use the Randomize statement ONCE in your program.
    Then, use RND() . RND generates a number between 0 and 1, so you multiply by the highest number - 1 that you want to choose. You can also set a lower limit.



    Rnd Function



    Returns a Single containing a random number.

    Syntax

    Rnd[(number)]

    The optional number argument is a Single or any valid numeric expression.

    Return Values

    If number is Rnd generates Less than zero The same number every time, using number as the seed. Greater than zero The next random number in the sequence. Equal to zero The most recently generated number. Not supplied The next random number in the sequence.
    Remarks

    The Rnd function returns a value less than 1 but greater than or equal to zero.

    The value of number determines how Rnd generates a random number:

    For any given initial seed, the same number sequence is generated because each successive call to the Rnd function uses the previous number as a seed for the next number in the sequence.

    Before calling Rnd, use the Randomize statement without an argument to initialize the random-number generator with a seed based on the system timer.

    To produce random integers in a given range, use this formula:

    Int((upperbound - lowerbound + 1) * Rnd + lowerbound)
    Here, upperbound is the highest number in the range, and lowerbound is the lowest number in the range.

    Note To repeat sequences of random numbers, call Rnd with a negative argument immediately before using Randomize with a numeric argument. Using Randomize with the same value for number does not repeat the previous sequence.

    Last edited by dglienna; December 12th, 2006 at 01:37 AM.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Feb 2004
    Posts
    218

    Re: how to generate a random number

    Thank you. Maybe I not state clearly, my meaning is that I have one list, says, 10,12,15,20,32 and so on, I just want to generate one random number from this list everytime, how can I do that?

  4. #4
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: how to generate a random number

    You could put them into a listbox, and then call a rnd listitem. Check out this silly sample.

    Code:
    Option Explicit
    
    Private Sub Command1_Click()
      Dim l As Integer
      l = List1.ListCount * Rnd
      MsgBox "Item: " & l & " = " & List1.List(l - 1)
    End Sub
    
    Private Sub Form_Load()
      Dim l As Integer
      Randomize
      For l = 2 To 15
    	List1.AddItem Int(Rnd * 7) + 1
      Next l
    End Sub
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  5. #5
    Join Date
    Nov 2006
    Posts
    16

    Re: how to generate a random number

    or you can store the values in an array and just random the index and call that value

  6. #6
    Join Date
    May 2002
    Posts
    10,943

    Re: how to generate a random number

    I would go with the array. Arrays have no visual control. They use less memory and are faster.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  7. #7
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: how to generate a random number

    I suggested the ListBox (which can be hidden) because of it's auto-sort property. Easier than sorting them and then choosing random.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  8. #8
    Join Date
    May 2002
    Posts
    10,943

    Re: how to generate a random number

    Interesting thought. I forgot that VB isn't like my PHP. PHP has so many array features built right in to the language.

    Good call dglienna.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  9. #9
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: how to generate a random number

    Please be advised that all of the aboive sugesstions will yield psuedo ranbom results. The generation of true random numbers is a science
    (and art) that many a doctorial thesis has been invested upon.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  10. #10
    Join Date
    Feb 2004
    Posts
    218

    Re: how to generate a random number

    thank you. how to delete one item in the list

  11. #11
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: how to generate a random number

    Always loop BACKWARDS if you need to delete more than one item.



    Removes an item from a ListBox or ComboBox control or a row from an MS Flex Grid control. Doesn't support named arguments.

    Syntax

    object.RemoveItem index

    The RemoveItem method syntax has these parts:

    Part Description object Required. An object expression that evaluates to an object in the Applies To list. index Required. Integer representing the position within the object of the item or row to remove. For the first item in a ListBox or ComboBox or for the first row in an MS Flex Grid control, index = 0.
    Remarks

    A ListBox or ComboBox that is bound to a Data control doesn't support the RemoveItem method.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  12. #12
    Join Date
    Oct 2006
    Posts
    327

    Re: how to generate a random number

    Sorry !....

    May you explain why to use a listbox ? (it's far from being necessary !)

    In France, we used to know a fireman (Mr Camembert) who had the verygood (bad ?) habit to make a new hole in his quarter just to fill an other hole, and so on...

    Want to see how to apply Me Camembert's method ? (It's a fast one !)

    Just say !

  13. #13
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: how to generate a random number

    Because you can set it to SORT the numbers, if they are being displayed. Makes finding dups easier.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  14. #14
    Join Date
    Feb 2004
    Posts
    218

    Re: how to generate a random number

    Remarks

    A ListBox or ComboBox that is bound to a Data control doesn't support the RemoveItem method.

    how to solve this? Thank you.

  15. #15
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: how to generate a random number

    Read the data, let the user make changes, and then write or delete records. A DELETE FROM query will delete a record, and if it's bound to a control, the control will be updated when you issue the UPDATE command.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured