Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Random Number

Random Number

Scheduled Pinned Locked Moved C#
csharphelptutorialquestionlounge
6 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    Imran Adam
    wrote on last edited by
    #1

    Hi I am new to cSharp and i would like to know how to generate a random number?? what code would i need to use?? I am aware of a method 'public virtual int Next();' However is there a easier way round this? Any help is much appreciated.

    Cheers :)

    A O D 3 Replies Last reply
    0
    • I Imran Adam

      Hi I am new to cSharp and i would like to know how to generate a random number?? what code would i need to use?? I am aware of a method 'public virtual int Next();' However is there a easier way round this? Any help is much appreciated.

      Cheers :)

      A Offline
      A Offline
      Alaric_
      wrote on last edited by
      #2

      ...you seem to be new to more than just C# ;) ..a simple search of "C# random" on the MSDN returns this at the top of their results..quite ironic I might add. I went to the MSDN to answer your question and Microsoft sent me back here lol. The solution provided in that article is probably overkill to your original question, but it is a great source for learning about random number generation

      "I need build Skynet. Plz send code"

      1 Reply Last reply
      0
      • I Imran Adam

        Hi I am new to cSharp and i would like to know how to generate a random number?? what code would i need to use?? I am aware of a method 'public virtual int Next();' However is there a easier way round this? Any help is much appreciated.

        Cheers :)

        O Offline
        O Offline
        originSH
        wrote on last edited by
        #3

        Random rnd = new Random(); rnd.Next(); easier than what? :P Two lines is neither Hard nor Complex. I suppose you could make your own method that would allow you to turn your 2 lines into 1, but I don't think it'll save you much devlopment time! Random needs a seed and so needs to be constructed as you might want the same seed as a previous time or a new seed. You then need to get each random number so a method call is needed, which also has multiple overloads so you can specifiy the number range if you wish. If you want to remove all the flexability provided by the different parameters you can pass to the constructor and method then go ahead :) but as I said is it really worth it to save a single line of code? Thats assuming you only use it once of course! the more times you use rnd.Next() without creating a new instance of rnd the difference in total lines gets less and less.

        Y 1 Reply Last reply
        0
        • O originSH

          Random rnd = new Random(); rnd.Next(); easier than what? :P Two lines is neither Hard nor Complex. I suppose you could make your own method that would allow you to turn your 2 lines into 1, but I don't think it'll save you much devlopment time! Random needs a seed and so needs to be constructed as you might want the same seed as a previous time or a new seed. You then need to get each random number so a method call is needed, which also has multiple overloads so you can specifiy the number range if you wish. If you want to remove all the flexability provided by the different parameters you can pass to the constructor and method then go ahead :) but as I said is it really worth it to save a single line of code? Thats assuming you only use it once of course! the more times you use rnd.Next() without creating a new instance of rnd the difference in total lines gets less and less.

          Y Offline
          Y Offline
          Yitzchok Dev
          wrote on last edited by
          #4

          Random rnd = new Random(); rnd.Next(); one line:)

          G 1 Reply Last reply
          0
          • I Imran Adam

            Hi I am new to cSharp and i would like to know how to generate a random number?? what code would i need to use?? I am aware of a method 'public virtual int Next();' However is there a easier way round this? Any help is much appreciated.

            Cheers :)

            D Offline
            D Offline
            Dan Neely
            wrote on last edited by
            #5

            Extending off of originSH's post, you generally don't want to reseed the generator each time. THe seed is taken from the system clock and as a result if you do multiple calls back to back within a few dozen milliseconds while reseeding before each number generated you'll get the same return for all of them.

            -- You have to explain to them [VB coders] what you mean by "typed". their first response is likely to be something like, "Of course my code is typed. Do you think i magically project it onto the screen with the power of my mind?" --- John Simmons / outlaw programmer

            1 Reply Last reply
            0
            • Y Yitzchok Dev

              Random rnd = new Random(); rnd.Next(); one line:)

              G Offline
              G Offline
              Guffa
              wrote on last edited by
              #6

              F16I wrote:

              Random rnd = new Random(); rnd.Next(); one line

              int r = new Random().Next(); really one line ;)

              --- single minded; short sighted; long gone;

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups