Home > Blockchain >  Picking array based off of percentage and shuffling it
Picking array based off of percentage and shuffling it

Time:12-21

I've got my percentages hammered out along with my arrays. I know I need to make it so that the percentage determines WHICH array is picked, and then I need to shuffle that array to make it spit out one of the three "things". I know there's an easier/more efficient way to do this without clogging my code with a million shuffle functions to determine the "Thing" variable.

Currently, it's not working (spits out "undefined") but it's left my scratching my head as I'm not sure what the issue is, along with wanting to streamline it.

The entire point of the code is to pick an array based on the rolled percentage, randomize that array, and spit back out the value it got from shuffling it.

Current absolute dumpster-fire I'm working with:

function generate(){

  var tierOne = ["thing one", "thing two", "thing three"]
  var tierTwo = ["thing four", "thing five", "thing six"]
  var tierThree = ["thing seven", "thing eight", "thing nine"]
  var tierFour = ["thing ten", "thing eleven", "thing twelve"]
  var tierFive = ["thing thirteen", "thing fourteen", "thing fifteen"]
  
    var percent = r();

    if (percent >= 0 && percent < 25) {
        shuffle(tierOne)
        thing = tierOne;
        return thing[0];
    } else if (percent >= 25 && percent < 36) {
        shuffle(tierTwo)
        thing = tierTwo;
        return thing[0];
    } else if (percent >= 36 && percent < 60) {
        shuffle(tierThree)
        thing = tierThree;
        return thing[0];
    } else if (percent >= 60 && percent < 76) {
        shuffle(tierFour)
        thing = tierFour;
        return thing[0];
    } else {
        shuffle(tierFive)
        thing = tierFive;
        return thing[0];
    }
} 

function r() {
    Math.floor(Math.random() * 100)   1;
    return Math.floor(Math.random() * 100)   1;
}```

CodePudding user response:

  1. Your shuffle routine should probably return a new array with the results.

  2. You need to declare thing, and not use a global variable.

if (percent >= 0 && percent < 20) {
        const thing = shuffle(tierOne)
        return thing[0];
    }

or

let thing
if (percent >= 0 && percent < 20) {
       thing = shuffle(tierOne)
        return thing[0];
    }

CodePudding user response:

First, I don't think there's any need to set up different arrays, then use that if-then-else logic where you compare the percentage to a range of values. Just make an array of arrays and use the index to return the one to shuffle. That also means that unless you really need the number from 1-100 for something else, then you should probably just generate a random number between 0 and 4. Assuming you need the percentage I left it in and just scale it to between 0 and 4.

I probably wouldn't have separated the shuffle logic from the generate function either, but I left it separate so you could more easily implement the shuffle logic you want. I don't believe there is a built in shuffle function in js like there is in some other languages, so you are going to have to have a shuffle function. Here is a thread on shuffling, and I shamelessly stole the shuffle function I included from it. Not saying this is the best one, just looked nifty. There is plenty of discussion in that post about the different implications of different shuffling algorithms.

How to randomize (shuffle) a JavaScript array?

console.log(generate());

function generate(){
  
  const raw = [
    ["thing one", "thing two", "thing three"],
    ["thing four", "thing five", "thing six"],
    ["thing seven", "thing eight", "thing nine"],
    ["thing ten", "thing eleven", "thing twelve"],
    ["thing thirteen", "thing fourteen", "thing fifteen"]
  ];
  
  var percent = Math.floor(Math.random() * 100)   1;
  var i = Math.ceil(percent/20)-1;
  return shuffle(raw[i])[0];
  
}
function shuffle(unshuffled){

  return unshuffled
    .map((value) => ({ value, sort: Math.random() }))
    .sort((a, b) => a.sort - b.sort)
    .map(({ value }) => value)
  ;
  
}

CodePudding user response:

First of, your function r has a duplicated line so you can simplify it to this:

function r() {
    return Math.floor(Math.random() * 100)   1;
}

I see you did not post your shuffle function so I guess you have this defined, or you can use one you can find here: https://stackoverflow.com/a/12646864/17537072

Finally, I would make this a matrix, this way you can have as many tiers as you want. Then use modulus to calculate the proper index based on the number of tiers you have. The code would result like this:

function generate(){
    let tiers = [
      ["thing one", "thing two", "thing three"],
      ["thing four", "thing five", "thing six"],
      ["thing seven", "thing eight", "thing nine"],
      ["thing ten", "thing eleven", "thing twelve"],
      ["thing thirteen", "thing fourteen", "thing fifteen"]
    ];
  
    var percent = r();
    
    return shuffle(tiers[percent % tiers.length]);
} 

tiers.length is the number of tiers, thus in your example 5. and then % makes any number between 0 and n-1 thus matching the index of the array.

I think this is short and dynamic.

  • Related