2

I have the following code:

public void onFinish() {
              int num;
              String outCome = "";
              for (int ii = 0; ii < 10; ii++) {
                  num = (int) (Math.random() * 100) % 3;

                  switch (num) {
                  case 0:
                      outCome = "ountcome1";
                      break;
                  case 1:
                      outCome = "outcome2";
                      break;
                  case 2:
                      outCome = "outcome3";
                      break;
             }
                text.setText(outCome);

What I'm trying to accomplish is that case 0 has a %50 chance to happen, while case 1 and 2 have %25 chance.

Thanks in advance!

3 Answers 3

1

Use: Random.nextInt(4) and in cases 0,3 return "ountcome1".
In case 1 return "ountcome2" and in case 2 return "ountcome3":

            switch (num) {
              case 0:
              case 3:
                  outCome = "outcome1";
                  break;
              case 1:
                  outCome = "outcome2";
                  break;
              case 2:
                  outCome = "outcome3";
                  break;
              ...          
4
  • So add another case then make 0 and 1 the same?
    – cflinspach
    Commented Jan 17, 2014 at 4:24
  • Math.random() returns a double which is why you have to cast it to int - that's not the correct way of using it. Use nextInt(4) and then you have 25% for each on of the numbers: [0,1,2,3] so you can treat 0 and 1 as the "first" 50% etc.
    – Nir Alfasi
    Commented Jan 17, 2014 at 4:25
  • public void onFinish() { int num; String outCome = ""; for (int ii = 0; ii < 10; ii++) { num = (int) (Random.nextInt(4); switch (num) { case 0: outCome = "outcome1"; break; case 1: outCome = "outcome2"; break; case 2: outCome = "outcome2"; break; case 3: outCome = "outcome3"; break; } text.setText(outCome); }
    – cflinspach
    Commented Jan 17, 2014 at 4:26
  • @Cflinspach posting code like you did in a comment is not readable. Better update your question! See updated code snippet in my answer.
    – Nir Alfasi
    Commented Jan 17, 2014 at 4:29
-1

Use this:

  for (int ii = 0; ii < 10; ii++) {
              num = (int) (Math.random() * 100) % 4; //Use 4 instead of 3

              switch (num) {
              case 0:
                  outCome = "ountcome1";
                  break;
              case 1:
                  outCome = "outcome2";
                  break;
              case 2:
                  outCome = "outcome3";
                  break;
              case 3:
                  outCome = "ountcome1";
                  break;

         }

Thus here there are four possible outcomes. If 0 or 3 are obtained, you output ountcome1 (50% probability) else the other two.

4
  • its not garenty to generte outcome 1 by 50% Commented Jan 17, 2014 at 4:28
  • there are only four possible outcomes here. 0,1,2, and 3 in random chance ( each equiprobable: 25% chance). If 0 or 3 are obtained we are outputting outcome 1. So thereby a 50% chance.
    – SoulRayder
    Commented Jan 17, 2014 at 4:30
  • Funny, it looks familiar, wait - that's my answer - only that I wrote it 5 minutes before you...
    – Nir Alfasi
    Commented Jan 17, 2014 at 4:34
  • No, you are using nextint, while I have used the same thing he used, changing only the divisor. So in case you downvoted my answer, please note that.
    – SoulRayder
    Commented Jan 17, 2014 at 4:35
-1

try to use this

public void onFinish() {
          int num;
          String outCome = "";
          for (int ii = 0; ii < 10; ii++) {


              Random x = new Random(Calendar.getInstance().getTimeInMilliSec());
              num = x.nextInt(4);
              switch (num) {
              case 0:
                  outCome = "ountcome1";
                  break;
              case 1:
                  outCome = "outcome2";
                  break;
              case 2:
                  outCome = "outcome3";
                  break;
              case 3:
                  outCome = "ountcome1";
                  break;
         }
            text.setText(outCome);
4
  • why has my answer being voted down can anyone explain Commented Jan 17, 2014 at 4:34
  • No it won't, the OP asked 50% for 0 then 25% for 1 and 25% for 2. That's not what you implemented.
    – Nir Alfasi
    Commented Jan 17, 2014 at 4:41
  • I think this would give 50% wieghtage to outcome one an 25, 25 % to outcome 2 and outcome 3 respectively Commented Jan 17, 2014 at 4:44
  • This last fix should work - only that the switch's body is an exact copy of Gautham's solution...
    – Nir Alfasi
    Commented Jan 17, 2014 at 4:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.