Results 1 to 13 of 13

Thread: [RESOLVED] Random Number

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2006
    Posts
    40

    Resolved [RESOLVED] Random Number

    Im making a number game and to do it I need the form to spit out any number between 1 and 100. I was able to make it spit out 70, but everytime the form loads, its always 70. Can someone show me a way to do it so that its a different number every time I start the form?
    Thanks

  2. #2
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Re: Random Number

    Use Randomize
    VB Code:
    1. Dim iValue As Integer
    2.     Randomize
    3.     iValue = Int((100 * Rnd) + 1)
    Use [code] source code here[/code] tags when you post source code.

    My Articles

  3. #3

    Thread Starter
    Member
    Join Date
    Feb 2006
    Posts
    40

    Re: Random Number

    Does it have to be Integer or can it be Double? Whats the difference?

  4. #4
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Re: Random Number

    Quote Originally Posted by StudNewf
    Does it have to be Integer or can it be Double? Whats the difference?
    It can be double too, but then what exactly do you want it to do. Are you trying to generate random numbers between 1 to 100 with decimal places. If that is the case then you can use
    VB Code:
    1. Dim iValue As Double
    2. Randomize
    3. iValue = (100 * Rnd) + 1
    Use [code] source code here[/code] tags when you post source code.

    My Articles

  5. #5
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    Re: Random Number

    VB Code:
    1. Public Function RandomRange(ByVal RangeStart As Int32, ByVal RangeEnd As Int32) As Int32
    2. Return New System.Random().Next(RangeStart, RangeEnd+1)
    3. End Function
    I converted it form C# in my head so hopefully it's right
    KrisSiegel.com - My Personal Website with my blog and portfolio
    Don't Forget to Rate Posts!

    Free Icons: FamFamFam, VBCorner, VBAccelerator
    Useful Links: System.Security.SecureString Managed DPAPI Overview Part 1 Managed DPAPI Overview Part 2 MSDN, MSDN2, Comparing the Timer Classes

  6. #6
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Re: Random Number

    Oh is it VB.NET.

    Till now I have been thinking it is VB 6.0. In .NET it is simple
    VB Code:
    1. Dim rndNumber As New Random
    2.         MessageBox.Show(rndNumber.Next(1, 100).ToString)

    Ignore my previous two posts. I was under impression that this thread was posted in Classic VB forum. Sorry about that.
    Use [code] source code here[/code] tags when you post source code.

    My Articles

  7. #7
    Fanatic Member
    Join Date
    Jan 2006
    Posts
    910

    Re: Random Number

    In .NET 2005 it can be done like this

    VB Code:
    1. label.text = CStr(Int(Rnd() *100))

  8. #8
    Frenzied Member Phill64's Avatar
    Join Date
    Jul 2005
    Location
    Queensland, Australia
    Posts
    1,201

    Re: Random Number

    Quote Originally Posted by jre1229
    In .NET 2005 it can be done like this

    VB Code:
    1. label.text = CStr(Int(Rnd() *100))
    That isn't a vb05 feature, it is legacy... comes from vb6, still supported in all version of .net thus far.

    the .net way is the above post, using the Random class.

  9. #9
    Fanatic Member
    Join Date
    Jan 2006
    Posts
    910

    Re: Random Number

    Sorry, Never used VB6.. Just stating a way that I know is possible in VB 2005.
    Quote Originally Posted by Phill64
    That isn't a vb05 feature, it is legacy... comes from vb6, still supported in all version of .net thus far.

    the .net way is the above post, using the Random class.

  10. #10

    Thread Starter
    Member
    Join Date
    Feb 2006
    Posts
    40

    Re: Random Number

    Thanks everybody, I think I got it down pat now. I really appreciate your help.
    Tahnks

  11. #11
    Frenzied Member vbdotnetboy's Avatar
    Join Date
    Jun 2004
    Location
    Lewisburg, PA "Next year Raiders in the Super Bowl"
    Posts
    1,310

    Re: Random Number

    please don't forget to mark the thread resolved if you've gotten the answer.

    Derek - Using VS 2008 99% of the time and VS 2003 1% of the time

    Please Help Us To Save Ana

    ● Helpful Links: DNR TV | Awesome site for tips | Using ADO.NET to work with Excel | Xml Namespace 2.0 Framework Changes|Ultra High Security Password Generator | Mendhak's ADO.NET Tutorial
    ● Code Bank: Random Password Generator | Generic DbProviderFactory Access
    ● Site Work: Bottle Run Xtreme | Spaids Racing.com

    Company I work for - CSSI

    WHEN POSTING PLEASE INDICATE VERSION

    Please use vbcode tags or code tags when posting code
    [highlight=vb]ALL your code goes here[/highlight] or [code]ALL your code goes here[/code]

    If my post helped you in anyway... please be kind and give me some ratings

  12. #12

    Thread Starter
    Member
    Join Date
    Feb 2006
    Posts
    40

    Re: Random Number

    How do I mark it resolved?

  13. #13

    Thread Starter
    Member
    Join Date
    Feb 2006
    Posts
    40

    Re: [RESOLVED] Random Number

    Nevermind I did it. Thanks

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