Ishitiaq Naser Uthsho
Ishitiaq Naser Uthsho

Reputation: 43

Making a rarity chance.in javascript

Hi i would like to know how to make a rarity chance in js? using Math function.

Lets say there's a 1% chance to get something like a hat.

how do i make it have 1% chance? and if i want to put like 50% how i do that?

I tried to use this

var gen = Math.floor(Math.random() * 100);
    var type = common;
      if(gen > 59) type = common;
      //if(gen < 16) type = alolans
      //if(gen < 10) type = galarians
      if(gen < 8) type = mythics;
      if(gen < 3) type = legends;
      if(gen < 2) type = ub; 

doesn't work please help. In the code I have put all the chances number how to make it work and get it rarely?

Upvotes: 1

Views: 2608

Answers (4)

Wow
Wow

Reputation: 1

function getType() {
    var gen = Math.floor(Math.random() * 100);
    console.log(gen);
    if (gen < 0.1) return 'Secret';
    if (gen < 3) return 'Mythical';
    if (gen < 9) return 'Legendary';
    if (gen < 23) return 'Super Rare';
    if (gen < 48) return 'Rare';
    if (gen < 71) return 'Uncommon';

    return 'common';
}

console.log(getType());

Upvotes: 0

Balastrong
Balastrong

Reputation: 4464

You can try this approach, comments in the code.

Just make sure that the sum of chances is below 100 and put one with 0 that will fill the remaining chances.

Compared to the other approaches, this allows you to easily add/remove rarities or change the chances without having to touch all the other values.

If you want to add one more with chances 8%, just add on the array

{type: 'oneMore', chance: 8}

and the job is done, everything still works :)

var rarities = [{
  type: "common",
  chance: 0
}, {
  type: "mythics",
  chance: 35
}, {
  type: "legends",
  chance: 20
}, {
  type: "ub",
  chance: 1
}];

function pickRandom() {
  // Calculate chances for common
  var filler = 100 - rarities.map(r => r.chance).reduce((sum, current) => sum + current);

  if (filler <= 0) {
    console.log("chances sum is higher than 100!");
    return;
  }

  // Create an array of 100 elements, based on the chances field
  var probability = rarities.map((r, i) => Array(r.chance === 0 ? filler : r.chance).fill(i)).reduce((c, v) => c.concat(v), []);

  // Pick one
  var pIndex = Math.floor(Math.random() * 100);
  var rarity = rarities[probability[pIndex]];

  console.log(rarity.type);
}

pickRandom();
pickRandom();
pickRandom();
pickRandom();
pickRandom();
pickRandom();

Upvotes: 3

Nina Scholz
Nina Scholz

Reputation: 386654

You need to sum the chances to get the right value for selecting a type.

function getType() {
    var gen = Math.floor(Math.random() * 100);
    console.log(gen);
    if (gen < 2) return 'ub';
    if (gen < 5) return 'legends';
    if (gen < 13) return 'mythics';
    if (gen < 23) return 'galarians';
    if (gen < 39) return 'alolans';

    return 'common';
}

console.log(getType());

Upvotes: 1

Marwen Jaffel
Marwen Jaffel

Reputation: 807

try this code :

var gen = Math.floor(Math.random() * 100);
var type = common;
  if(gen > 59) type = common;
  else if(gen < 2) type = ub;
  else if(gen < 3) type = legends;
  else if(gen < 8) type = mythics;

start the if statstatment (<) by the lower

Upvotes: 0

Related Questions