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. How do you create 2 random values [modified]

How do you create 2 random values [modified]

Scheduled Pinned Locked Moved C#
questionlounge
4 Posts 4 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.
  • N Offline
    N Offline
    Nathan Revka
    wrote on last edited by
    #1

    I wrote

            Random RollOne = new Random();
            Random RollTwo = new Random();
                dieOne = RollOne.Next(1, 7);
                dieTwo = RollTwo.Next(1 , 7);
                sum = dieOne + dieTwo;
    

    but dieOne and dieTwo are always equal, how do you make two independent random values? Edit: Thanks Mikanu, it works now.

    modified on Monday, July 13, 2009 10:43 PM

    M C P 3 Replies Last reply
    0
    • N Nathan Revka

      I wrote

              Random RollOne = new Random();
              Random RollTwo = new Random();
                  dieOne = RollOne.Next(1, 7);
                  dieTwo = RollTwo.Next(1 , 7);
                  sum = dieOne + dieTwo;
      

      but dieOne and dieTwo are always equal, how do you make two independent random values? Edit: Thanks Mikanu, it works now.

      modified on Monday, July 13, 2009 10:43 PM

      M Offline
      M Offline
      mikanu
      wrote on last edited by
      #2

      The correct way to do that is to create an instance of the Random class and then call one of the following methods: - Next(Int32 max) // will return a random number smaller than _max_ - NextByte() // will return a random byte (between 0 and 255) - NextDouble() // will return a random number between 0.0 and 1.0 Here's a code sample which will generate two random numbers, a and b, between 0 and 10

      Random rnd = new Random();
      int a = rnd.Next(10);
      int b = rng.Next(10);

      ---- <a href="www.mdinescu.com">www.mdinescu.com</a>

      1 Reply Last reply
      0
      • N Nathan Revka

        I wrote

                Random RollOne = new Random();
                Random RollTwo = new Random();
                    dieOne = RollOne.Next(1, 7);
                    dieTwo = RollTwo.Next(1 , 7);
                    sum = dieOne + dieTwo;
        

        but dieOne and dieTwo are always equal, how do you make two independent random values? Edit: Thanks Mikanu, it works now.

        modified on Monday, July 13, 2009 10:43 PM

        C Offline
        C Offline
        Christian Graus
        wrote on last edited by
        #3

        To add to the other post ( which was correct ), there is no such thing as a random number to a computer. It only looks that way. A random sequence is started by what's called a seed value. You can specify a seed value to regenerate the same pseudo random sequence if you need to. If you don't specify one, the current date and time are used. As you can see, you created two random objects with the same seed, so they will both start on the same number.

        Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

        1 Reply Last reply
        0
        • N Nathan Revka

          I wrote

                  Random RollOne = new Random();
                  Random RollTwo = new Random();
                      dieOne = RollOne.Next(1, 7);
                      dieTwo = RollTwo.Next(1 , 7);
                      sum = dieOne + dieTwo;
          

          but dieOne and dieTwo are always equal, how do you make two independent random values? Edit: Thanks Mikanu, it works now.

          modified on Monday, July 13, 2009 10:43 PM

          P Offline
          P Offline
          PIEBALDconsult
          wrote on last edited by
          #4

          You should create one instance of Random as a static field of the class and use it whenever you need it. It should never be a local variable of a method.

          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