Results 1 to 10 of 10

Thread: Easy Random Number Function (Min, Max & Round)

  1. #1

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Easy Random Number Function (Min, Max & Round)

    This functions makes getting random whole/decimal numbers easy. It also automatically calls Randomize the first time the function is called, so there is no need to put it in the Form_Load Procedure.
    VB Code:
    1. ' Place this code in a module.
    2. ''''''''''''''''''''''''''''''''''
    3. ' Date:         7/2/2005         '
    4. ' Last Update:  7/29/2005        '
    5. ' Author:       Jeremy Blanchard '
    6. ''''''''''''''''''''''''''''''''''
    7. Option Explicit
    8.  
    9. Public Function Rand(Max As Double, _
    10.             Optional Min As Double = 0, _
    11.             Optional NumDigitsAfterDecimal As Integer = 0) As Double
    12.   ' Returns a pseudo-random number  between 'Min' and 'Max'.
    13.   '   Calls Randomize the first time the function is called.
    14.  
    15.     Static sbRandomized As Boolean
    16.     Dim r As Double
    17.  
    18.     If Not sbRandomized Then
    19.         Randomize
    20.         sbRandomized = True
    21.     End If
    22.  
    23.     If NumDigitsAfterDecimal <= 0 Then
    24.         Rand = Int(Rnd * (Max - Min + 1)) + Min
    25.     Else
    26.         ' Less accurate number distrubtion because of decimals.
    27.         Rand = Round((Rnd * (Max - Min)) + Min, NumDigitsAfterDecimal)
    28.     End If
    29. End Function
    VB Code:
    1. ' Example uses
    2.   ...
    3.   r = Rand(255)
    4.   g = Rand(255)
    5.   b = Rand(255)
    6.   Form1.BackColor = RGB(r, g, b)   ' Random background color
    7.   ...
    8.   iMyNum = Rand(10, -10, 3)  ' Rounded to 3 decimal places
    NOTE: Update on 7/29/05 switched max to the first parameter and Min to the second (and made it default optional).

    Let me know if you like this module or used it in a cool way.
    Last edited by eyeRmonkey; Jul 29th, 2005 at 09:46 PM. Reason: Modifications to module
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  2. #2
    Frenzied Member Inuyasha1782's Avatar
    Join Date
    May 2005
    Location
    California, USA
    Posts
    1,035

    Re: Easy Random Number Function (Min, Max & Round)

    A random timer:

    VB Code:
    1. '''''''''''''''''''Random Timer'''''''''''''''''''''''''''''''''''''''
    2. 'Credits: EyeRmonkey for the random module
    3. 'Created By: AliasXNeo
    4. 'Public use
    5.  
    6. Option Explicit
    7.  
    8. Private Sub Command1_Click()
    9. If Command1.Caption = "Stop Timer" Then 'Checking if it's running already
    10. 'If it is, disable it
    11. Timer1.Enabled = False
    12. Timer2.Enabled = False
    13. Command1.Caption = "Start Timer"
    14.  
    15. Else 'If it isn't:
    16.  
    17. Command1.Caption = "Stop Timer" 'Make sure they can stop it
    18. 'Randomize the time between
    19. Label2.Caption = rand(1, 10)
    20. Label4.Caption = rand(1, 10)
    21. 'Set the timer interval random from the two random results, and times by 1000 for seconds
    22. Timer1.Interval = rand(Label2.Caption, Label4.Caption) * 1000
    23. 'Set the countdown timer, and format it
    24. Label6.Caption = Timer1.Interval / 1000
    25. 'Enabled everything
    26. Timer1.Enabled = True
    27.  
    28. End If
    29. End Sub
    30.  
    31. Private Sub Timer1_Timer()
    32. 'Randomize to get the between
    33. Label2.Caption = rand(1, 10)
    34. Label4.Caption = rand(1, 10)
    35. 'Randomize our two results, and times by 1000 for seconds
    36. Timer1.Interval = rand(Label2.Caption, Label4.Caption) * 1000
    37. Label6.Caption = Timer1.Interval / 1000
    38. End Sub

    It's good for the kind of stuff I program

  3. #3
    Frenzied Member wiz126's Avatar
    Join Date
    Jul 2005
    Location
    Mars,Milky Way... Chit Chat Posts: 5,733
    Posts
    1,080

    Re: Easy Random Number Function (Min, Max & Round)

    what are you trying to do?
    make a form backcolor to changed every time it opend?



    .
    1) If your post has been adequately answered please click in your post on "Mark Thread Resolved".
    2) If someone has been useful to you please show your respect by rating their posts.
    3) Please use [highlight="VB"] 'your code goes in here [/highlight] tags when posting code.
    4) Before posting your question, make sure you checked this links:
    MICROSOFT MSDN -- VB FORUMS SEARCH

    5)Support Classic VB - A PETITION TO MICROSOFT

    ___________________________________________________________________________________
    THINGS TO KNOW ABOUT VB: || VB Examples/Demos
    What are Classes?
    || -
    Where to place a sub/function?(global) || Webbrowser control

  4. #4

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: Easy Random Number Function (Min, Max & Round)

    Yeah, I'm lost on that one. I can't believe I ever posted this. But while I'm here I will post an updated version that allows decimals.
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  5. #5
    Frenzied Member wiz126's Avatar
    Join Date
    Jul 2005
    Location
    Mars,Milky Way... Chit Chat Posts: 5,733
    Posts
    1,080

    Re: Easy Random Number Function (Min, Max & Round)

    wow !! your code is way way to long

    VB Code:
    1. Private Sub Command1_Click()
    2. Randomize
    3.  r = Int(255 * Rnd + 0)
    4.   g = Int(255 * Rnd + 0)
    5.   b = Int(255 * Rnd + 0)
    6.   Form1.BackColor = RGB(r, g, b)
    7.  
    8. End Sub
    9.  
    10. Private Sub Form_Load()
    11. Randomize
    12.  r = Int(255 * Rnd + 0)
    13.   g = Int(255 * Rnd + 0)
    14.   b = Int(255 * Rnd + 0)
    15.   Form1.BackColor = RGB(r, g, b)
    16.  
    17. End Sub
    1) If your post has been adequately answered please click in your post on "Mark Thread Resolved".
    2) If someone has been useful to you please show your respect by rating their posts.
    3) Please use [highlight="VB"] 'your code goes in here [/highlight] tags when posting code.
    4) Before posting your question, make sure you checked this links:
    MICROSOFT MSDN -- VB FORUMS SEARCH

    5)Support Classic VB - A PETITION TO MICROSOFT

    ___________________________________________________________________________________
    THINGS TO KNOW ABOUT VB: || VB Examples/Demos
    What are Classes?
    || -
    Where to place a sub/function?(global) || Webbrowser control

  6. #6

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: Easy Random Number Function (Min, Max & Round)

    I think you are missing the point. It is suppose to be all purpose. As in I can put that module into anything and use it to get any kind of random number. If all I wanted to do was get 3 random numbers then that would be fine.

    Also, you don't need + 0 after everything.
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  7. #7
    Frenzied Member wiz126's Avatar
    Join Date
    Jul 2005
    Location
    Mars,Milky Way... Chit Chat Posts: 5,733
    Posts
    1,080

    Re: Easy Random Number Function (Min, Max & Round)

    Quote Originally Posted by eyeRmonkey
    I think you are missing the point. It is suppose to be all purpose. As in I can put that module into anything and use it to get any kind of random number. If all I wanted to do was get 3 random numbers then that would be fine.

    Also, you don't need + 0 after everything.

    yes you do need 0 after every single rendom generated number
    1) If your post has been adequately answered please click in your post on "Mark Thread Resolved".
    2) If someone has been useful to you please show your respect by rating their posts.
    3) Please use [highlight="VB"] 'your code goes in here [/highlight] tags when posting code.
    4) Before posting your question, make sure you checked this links:
    MICROSOFT MSDN -- VB FORUMS SEARCH

    5)Support Classic VB - A PETITION TO MICROSOFT

    ___________________________________________________________________________________
    THINGS TO KNOW ABOUT VB: || VB Examples/Demos
    What are Classes?
    || -
    Where to place a sub/function?(global) || Webbrowser control

  8. #8
    Frenzied Member Inuyasha1782's Avatar
    Join Date
    May 2005
    Location
    California, USA
    Posts
    1,035

    Re: Easy Random Number Function (Min, Max & Round)

    My code was simply just making a completely random timer. I was frankly bored and was just thinking of something to make with it, and I came up with that. Make it yourself, as you can see it gets two random numbers, then get another random number betweeen those two random numbers giving a very smooth random. For the stuff I do, it's very handy.

  9. #9
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Easy Random Number Function (Min, Max & Round)

    Quote Originally Posted by wiz126
    yes you do need 0 after every single rendom generated number
    If you actually think about it, it's completely pointless

  10. #10
    Member
    Join Date
    Jul 2005
    Posts
    35

    Re: Easy Random Number Function (Min, Max & Round)

    well the way he did it you must have the 0 or it will b wrong

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