-
December 12th, 2006, 12:48 AM
#1
how to generate a random number
Hi, everyone,
How to generate a random number from a given list? Thank you
-
December 12th, 2006, 01:30 AM
#2
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.
-
December 12th, 2006, 10:51 AM
#3
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?
-
December 12th, 2006, 11:13 AM
#4
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
-
December 12th, 2006, 11:24 AM
#5
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
-
December 12th, 2006, 12:18 PM
#6
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.
-
December 12th, 2006, 03:11 PM
#7
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.
-
December 12th, 2006, 03:22 PM
#8
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.
-
December 12th, 2006, 03:31 PM
#9
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
-
December 13th, 2006, 04:08 AM
#10
Re: how to generate a random number
thank you. how to delete one item in the list
-
December 13th, 2006, 12:21 PM
#11
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.
-
December 13th, 2006, 02:41 PM
#12
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 !
-
December 13th, 2006, 06:45 PM
#13
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.
-
December 13th, 2006, 10:21 PM
#14
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.
-
December 13th, 2006, 11:33 PM
#15
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|