Random

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Efrain

    #1

    Random

    Can somebody help me out? I need the code to generate a random number between
    1 and 100.
  • Scott M.

    #2
    Re: Random

    Dim randValue As Integer
    Dim x As New Random
    randValue = x.Next(1001)

    The documentation on the Next method does not state the very important fact
    that the value you pass as the maximum value the random class should return
    (in this case 1001) is never reached. So, if you want to potentially get
    back 1000, you must enter 1001.



    "Efrain" <Efrain@discuss ions.microsoft. com> wrote in message
    news:DCB1B99A-4B0A-452B-9D88-1282C509CDF7@mi crosoft.com...[color=blue]
    > Can somebody help me out? I need the code to generate a random number
    > between
    > 1 and 100.[/color]


    Comment

    • Peter van der Goes

      #3
      Re: Random


      "Efrain" <Efrain@discuss ions.microsoft. com> wrote in message
      news:DCB1B99A-4B0A-452B-9D88-1282C509CDF7@mi crosoft.com...[color=blue]
      > Can somebody help me out? I need the code to generate a random number
      > between
      > 1 and 100.[/color]

      Scott gave you VB code. Here's the C# equivalent.

      Random myR = new Random();

      int x = myR.Next(1,101) ;

      The C++ code would depend on the type of project, i.e. the .NET Random class
      would be appropriate in a .NET WinForms app, but not in a MFC app.

      What language are you using?


      --
      Peter [MVP Visual Developer]
      Jack of all trades, master of none.


      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: Random

        Scott M. <s-mar@nospam.nosp am> wrote:[color=blue]
        > Dim randValue As Integer
        > Dim x As New Random
        > randValue = x.Next(1001)
        >
        > The documentation on the Next method does not state the very important fact
        > that the value you pass as the maximum value the random class should return
        > (in this case 1001) is never reached. So, if you want to potentially get
        > back 1000, you must enter 1001.[/color]

        However, the lower bound should be 0 here, so actually what's wanted is

        x.Next(100)+1

        (or x.Next(1, 101))

        --
        Jon Skeet - <skeet@pobox.co m>
        Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

        If replying to the group, please do not mail me too

        Comment

        Working...