|
-
Mar 9th, 2003, 03:27 PM
#1
Thread Starter
Hyperactive Member
random number?
please i want to generate random numbers between 1 and 52,i was using someting like this:
randomize
x=int((p)*rnd)
but wasn't getting the desired results
the rnd is not in this format:- 0.rrr ie i just need the three digits after the decimal.
i was seeing something like :-0.rrrrrrrrrrrrrrr and some in negative exponent.i need numbers between 1 and 52 shuffled randomly
-
Mar 9th, 2003, 03:37 PM
#2
-= B u g S l a y e r =-
VB Code:
Private Sub Command1_Click()
Randomize
Debug.Print Int(Format(52 * Rnd + 1))
End Sub
-
Mar 9th, 2003, 03:41 PM
#3
VB Code:
Dim nTry As Integer
Dim nAddCount As Integer
Dim bFound As Integer
Dim nDummy As Integer
Dim MyCollection As New Collection
Randomize
Do Until nAddCount = 52
nTry = Int((52) * Rnd + 1)
On Error Resume Next
nDummy = MyCollection.Item(CStr(nTry))
bFound = (Err = 0)
If bFound Then
Err.Clear
Else
MyCollection.Add nTry, CStr(nTry)
nAddCount = nAddCount + 1
End If
Loop
When you want to use the values remember that collections start at 1 and not 0.
-
Mar 9th, 2003, 03:59 PM
#4
-
Mar 9th, 2003, 04:02 PM
#5
-
Mar 9th, 2003, 04:06 PM
#6
Originally posted by peet
what did I miss ??
I assumed he wanted to shuffle a deck of cards so I gave him a solution that would produce the numbers 1 to 52 without repetition.
-
Mar 9th, 2003, 04:08 PM
#7
Frenzied Member
Originally posted by manavo11
Peet, it should be : 51 * rnd - 1 . If you want a random number between two other specific ones (hypothetically a,b) then you will get it from (b - a) * RND + a.
Actually, Peet was correct. From VB Help:
Rnd Function Example
This example uses the Rnd function to generate a random integer value from 1 to 6.
VB Code:
Dim MyValue
MyValue = Int((6 * Rnd) + 1) ' Generate random value between 1 and 6.
-
Mar 9th, 2003, 04:10 PM
#8
-= B u g S l a y e r =-
Originally posted by manavo11
Peet, it should be : 51 * rnd - 1 . If you want a random number between two other specific ones (hypothetically a,b) then you will get it from (b - a) * RND + a.
no,
VB Code:
Debug.Print Int(Format(52 * Rnd + 1))
is correct , I was reffering to Marty's coll code
-
Mar 9th, 2003, 04:11 PM
#9
-= B u g S l a y e r =-
Originally posted by MartinLiss
I assumed he wanted to shuffle a deck of cards so I gave him a solution that would produce the numbers 1 to 52 without repetition.
I see
-
Mar 9th, 2003, 04:12 PM
#10
-= B u g S l a y e r =-
uhh... loose the Format btw
VB Code:
Debug.Print Int(52 * Rnd + 1)
-
Mar 9th, 2003, 04:15 PM
#11
-
Mar 9th, 2003, 04:22 PM
#12
-= B u g S l a y e r =-
no, using my statement will never result in 53.
rnd will never return 1
VB Code:
Private Sub Command1_Click()
Dim i As Integer
Randomize
i = Int(52 * Rnd + 1)
While i <> 53
Debug.Print i
i = Int(52 * Rnd + 1)
Wend
End Sub
the loop will never end.
-
Mar 9th, 2003, 04:33 PM
#13
-
Mar 9th, 2003, 05:45 PM
#14
From Help
The Rnd function returns a value less than 1 but greater than or equal to zero.
-
Mar 9th, 2003, 05:55 PM
#15
-
Mar 10th, 2003, 02:38 AM
#16
Addicted Member
MSDN also says this for the general solution:
Int((upperbound - lowerbound + 1) * Rnd + lowerbound)
So for numbers between 1 and 52 (inclusive I assume) upperbound is 52, lowerbound is 1, so you get
VB Code:
'Int((52-1+1)*Rnd + 1)
Int (52 * Rnd + 1)
as Peet said.
-
Mar 10th, 2003, 07:43 AM
#17
-
Mar 10th, 2003, 08:21 AM
#18
Originally posted by MartinLiss
VB Code:
Dim nTry As Integer
Dim nAddCount As Integer
Dim bFound As Integer
Dim nDummy As Integer
Dim MyCollection As New Collection
Randomize
Do Until nAddCount = 52
nTry = Int((52) * Rnd + 1)
On Error Resume Next
nDummy = MyCollection.Item(CStr(nTry))
bFound = (Err = 0)
If bFound Then
Err.Clear
Else
MyCollection.Add nTry, CStr(nTry)
nAddCount = nAddCount + 1
End If
Loop
When you want to use the values remember that collections start at 1 and not 0.
That's a bit over the top Marty Here's a shortened version of the same code:
VB Code:
Dim nTry As Integer
Dim MyCollection As Collection
Set MyCollection = New Collection
Randomize
On Error Resume Next
Do Until MyCollection.Count = 52
nTry = Int((52) * Rnd + 1)
MyCollection.Add nTry, CStr(nTry)
Loop
Err.Clear
-
Mar 10th, 2003, 11:45 AM
#19
Thanks, that is an improvement and both changes you made (using the collection's count as the limit and eliminating the unnecessary error checking) are obvious if you think about it.
-
Mar 10th, 2003, 11:59 AM
#20
Originally posted by MartinLiss
Thanks, that is an improvement and both changes you made (using the collection's count as the limit and eliminating the unnecessary error checking) are obvious if you think about it.
No problem, I'll use it myself at some point I get used to seeing code around here that is far too long, as I'm sure you know some VB beginners write code that does over 5 times as much as needed.
It's nice to be able to 'correct' somebody who knows what they are doing - even tho it's just because they didn't optimise it fully
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
|