0

I'm currently working on a small game that needs to return either 1,2 or 3.

The idea is that I then compare two numbers and the greater wins.

The code i'm using is this:

var isMultipleOf, number1, number2, random;

random = function(number) {
  return isMultipleOf(Math.floor((Math.random() * number) + 1));
};

isMultipleOf = function(number) {
  if (number % 2 === 0) {
    return 1;
  }
  if (number % 3 === 0) {
    return 2;
  } else {
    return 3;
  }
};

number1 = random((Math.random() * 100) + 1);
number2 = random((Math.random() * 100) + 1);

console.log("number 1 is " + number1 + " and number2 is " + number2);

This code works, but i'd like to improve it a bit if possible.

With my game, a draw is possible, but with my current logic it happens quite often and I don't like it. Can you suggest a better way to improve this making it less possible to get a draw (same numbers)?

thanks

10
  • 4
    I'm voting to close this question as off-topic because it refers to working code, and therefore should be posted to the Code Review SE site. Commented Aug 3, 2017 at 15:44
  • 1
    There's plenty of questions here on SO asking for better way to do things, i don't see why this would be off-topic to be honest Commented Aug 3, 2017 at 15:45
  • 1
    The existence of off-topic questions which haven't been closed yet doesn't make another off-topic question on topic. Commented Aug 3, 2017 at 15:48
  • 1
    isMultipleOf doesn't return 1, 2 or 3 with the same frequency Commented Aug 3, 2017 at 15:48
  • 1
    Logic for 1,2,3 seem off to me.... why not say if < .33, <.66 Commented Aug 3, 2017 at 15:49

3 Answers 3

1

The reason why your code has so many ties is your distribution is really messed up.

isMultipleOf = function(number) {
  if (number % 2 === 0) {
    return 1;
  }
  if (number % 3 === 0) {
    return 2;
  } else {
    return 3;
  }
};

var results = [0,0,0];
for (var i=1; i<=100; i ++ ){
   results[isMultipleOf(i)-1] += 1
}

console.log(results)

Chance of 1: 50
Chance of 2: 17
Chance of 3: 33

If you want better results, than just use random and divide it into 3 parts.

getNum = function(number) {
  if (number < .33) {
    return 1;
  }
  if (number < .67) {
    return 2;
  } else {
    return 3;
  }
};

var results = [0,0,0];
for (var i=1; i<=100; i ++ ){
   var rn = Math.random()
   results[getNum(rn)-1] += 1
}

console.log(results)

Sign up to request clarification or add additional context in comments.

Comments

1

Currently you select a random number from 1 to 100, select another random number less than or equal to the first, and then if the second number is a multiple of 2 return 1, multiple of 3 but not 2 return 2, else return 3.

Why not just pick two random numbers between 1 and 3?

var rnd = Math.floor(Math.random() * 3) + 1;  // 1 2 or 3, with 1/3 probability

Another option is for each of the numbers to be drawn from a known pool (like a lottery), guaranteeing there are no ties.

//start with an ordered array
var nums = [1,2,3];

// fisher-yates shuffle
for (var i=nums.length - 1; i > 0; i--) {
  var r = Math.floor(Math.random() * (i + 1));
  var tmp = nums[r];
  nums[r] = nums[i];
  nums[i] = tmp;
}

// any two elements can be our drawn numbers
alert("first: " + nums[0] + ", second: " + nums[1]);

Comments

0

First of all, do you want to have a draw or maybe it's better just to make another randomization when you have it until there will be winning side or just make it another time to lower chances for a draw? The easiest way would be of course adding more number so statistically there would be fewer chances for a draw.

1 Comment

I was trying to add more numbers with my code, cause the idea behind was that the more random number i've got the less chances of a draw. It can be possible, but now it can happen 5-6 times in a row which i don't really like

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.