Hi, everyone,
How to generate a random number from a given list? Thank you
Printable View
Hi, everyone,
How to generate a random number from a given list? Thank you
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.
Quote:
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.
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?
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
or you can store the values in an array and just random the index and call that value
I would go with the array. Arrays have no visual control. They use less memory and are faster.
I suggested the ListBox (which can be hidden) because of it's auto-sort property. Easier than sorting them and then choosing random.
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. :thumb:
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.
thank you. how to delete one item in the list
Always loop BACKWARDS if you need to delete more than one item.
Quote:
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.
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 ! :wave:
Because you can set it to SORT the numbers, if they are being displayed. Makes finding dups easier.
Remarks
A ListBox or ComboBox that is bound to a Data control doesn't support the RemoveItem method.
how to solve this? Thank you.
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.