Results 1 to 20 of 20

Thread: random number?

  1. #1

    Thread Starter
    Hyperactive Member oyad's Avatar
    Join Date
    Feb 2003
    Location
    PhoxWare MicroSystems
    Posts
    463

    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

  2. #2
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    VB Code:
    1. Private Sub Command1_Click()
    2.     Randomize
    3.     Debug.Print Int(Format(52 * Rnd + 1))
    4. End Sub
    -= a peet post =-

  3. #3
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    VB Code:
    1. Dim nTry As Integer
    2.     Dim nAddCount As Integer
    3.     Dim bFound As Integer
    4.     Dim nDummy As Integer
    5.  
    6.     Dim MyCollection As New Collection
    7.    
    8.     Randomize
    9.    
    10.     Do Until nAddCount = 52
    11.         nTry = Int((52) * Rnd + 1)
    12.         On Error Resume Next
    13.         nDummy = MyCollection.Item(CStr(nTry))
    14.         bFound = (Err = 0)
    15.         If bFound Then
    16.             Err.Clear
    17.         Else
    18.             MyCollection.Add nTry, CStr(nTry)
    19.             nAddCount = nAddCount + 1
    20.         End If
    21.     Loop
    When you want to use the values remember that collections start at 1 and not 0.

  4. #4
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    what did I miss ??
    -= a peet post =-

  5. #5
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171
    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.


    Has someone helped you? Then you can Rate their helpful post.

  6. #6

  7. #7
    Frenzied Member
    Join Date
    Jan 2000
    Location
    Bellevue, WA, USA
    Posts
    1,357
    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:
    1. Dim MyValue
    2. MyValue = Int((6 * Rnd) + 1)   ' Generate random value between 1 and 6.
    ~seaweed

  8. #8
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    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:
    1. Debug.Print Int(Format(52 * Rnd + 1))
    is correct , I was reffering to Marty's coll code
    -= a peet post =-

  9. #9
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    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
    -= a peet post =-

  10. #10
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    uhh... loose the Format btw


    VB Code:
    1. Debug.Print Int(52 * Rnd + 1)
    -= a peet post =-

  11. #11
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171
    Peet, if I am not mistaken RND generates a random number between 0 and 1. So in your case, if you get the minimum value you get 1 as a result. But if you get the maximum, you get 53 as a result. Right?


    Has someone helped you? Then you can Rate their helpful post.

  12. #12
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    no, using my statement will never result in 53.
    rnd will never return 1

    VB Code:
    1. Private Sub Command1_Click()
    2.     Dim i As Integer
    3.     Randomize
    4.     i = Int(52 * Rnd + 1)
    5.     While i <> 53
    6.         Debug.Print i
    7.         i = Int(52 * Rnd + 1)
    8.     Wend
    9. End Sub

    the loop will never end.
    -= a peet post =-

  13. #13
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171
    Well, if RND never returns the value 1 then it is my mistake.


    Has someone helped you? Then you can Rate their helpful post.

  14. #14

  15. #15
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171
    Thanks Martin. I never noticed that.


    Has someone helped you? Then you can Rate their helpful post.

  16. #16
    Addicted Member
    Join Date
    Nov 2002
    Posts
    155
    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:
    1. 'Int((52-1+1)*Rnd + 1)
    2.  
    3. Int (52 * Rnd + 1)

    as Peet said.

  17. #17
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171
    OK I got it! I just thought that RND returned the value 1.


    Has someone helped you? Then you can Rate their helpful post.

  18. #18
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    Originally posted by MartinLiss
    VB Code:
    1. Dim nTry As Integer
    2.     Dim nAddCount As Integer
    3.     Dim bFound As Integer
    4.     Dim nDummy As Integer
    5.  
    6.     Dim MyCollection As New Collection
    7.    
    8.     Randomize
    9.    
    10.     Do Until nAddCount = 52
    11.         nTry = Int((52) * Rnd + 1)
    12.         On Error Resume Next
    13.         nDummy = MyCollection.Item(CStr(nTry))
    14.         bFound = (Err = 0)
    15.         If bFound Then
    16.             Err.Clear
    17.         Else
    18.             MyCollection.Add nTry, CStr(nTry)
    19.             nAddCount = nAddCount + 1
    20.         End If
    21.     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:
    1. Dim nTry As Integer
    2. Dim MyCollection As Collection
    3.  
    4.   Set MyCollection = New Collection
    5.  
    6.   Randomize
    7.  
    8.   On Error Resume Next
    9.   Do Until MyCollection.Count = 52
    10.       nTry = Int((52) * Rnd + 1)
    11.       MyCollection.Add nTry, CStr(nTry)
    12.   Loop
    13.   Err.Clear

  19. #19

  20. #20
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    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
  •  



Click Here to Expand Forum to Full Width