java giving 50% chance

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • wassssup
    New Member
    • Oct 2007
    • 47

    java giving 50% chance

    i know math.random can randomly generate numbers, but i want to know is there a way to let the system decides which variable to choose from a and b?
    i want to set both can randomly be selected (50 - 50) chance?
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by wassssup
    i know math.random can randomly generate numbers, but i want to know is there a way to let the system decides which variable to choose from a and b?
    i want to set both can randomly be selected (50 - 50) chance?
    Math.random generates numbers between 0.0 and 1.0
    Half of that is 0.5.
    Ideally then, numbers in [0.0, 0.5) have a 50% chance of getting generated.

    Comment

    • wassssup
      New Member
      • Oct 2007
      • 47

      #3
      Originally posted by r035198x
      Math.random generates numbers between 0.0 and 1.0
      Half of that is 0.5.
      Ideally then, numbers in [0.0, 0.5) have a 50% chance of getting generated.
      yeah...but is there other way then math.random? just curious..

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by wassssup
        yeah...but is there other way then math.random? just curious..
        java.util.Rando m ?

        Comment

        • BigDaddyLH
          Recognized Expert Top Contributor
          • Dec 2007
          • 1216

          #5
          Originally posted by r035198x
          java.util.Rando m?
          Indeed. Method Math.random just uses java.util.Rando m anyway, and the class has a bunch of useful methods. Method Math.random is just leftover from the early API. When you think you need a source of randomness you should check out class Random first.

          Comment

          • drsmooth
            New Member
            • Oct 2007
            • 112

            #6
            if u use the java.util.Rando m, you could use

            [CODE=cpp]Random r = new Random();
            int choice = r.nextInt(2);
            if(choice==0)
            //choice 1
            else
            //choice b[/CODE]

            thats what i would recommend, the nextInt(2); would generate either a 1 or a 0, at what i would assume is a 50-50 chance.

            Comment

            • BigDaddyLH
              Recognized Expert Top Contributor
              • Dec 2007
              • 1216

              #7
              Originally posted by drsmooth
              if u use the java.util.Rando m, you could use

              [CODE=Java]Random r = new Random();
              int choice = r.nextInt(2);
              if(choice==0)
              //choice 1
              else
              //choice b[/CODE]

              thats what i would recommend, the nextInt(2); would generate either a 1 or a 0, at what i would assume is a 50-50 chance.
              And if it's a turn-left/turn-right you'd be wanting, there's a method for that in Random. No need to generate 0/1 and then turn it into true/false:
              [CODE=Java]if (rnd.nextBoolea n()) {
              ...
              } else {
              ...
              }[/CODE]

              Comment

              Working...