0

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;
}```
  • Can you clear up what is the problem? do you want to optimize your code or does your current code does not work? – Daniel Cruz Dec 21 '21 at 01:38
  • Threw an edit up to clarify that my code doesn't work *and* I'd wish to optimize it. Didn't realize I hadn't added that to my original post, thank you for pointing it out! @Daniel Cruz – BadCodingRat Dec 21 '21 at 01:49
  • Shuffling and taking only the first item is just like picking at a random index. – danh Dec 21 '21 at 02:14

2 Answers2

1

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)
  ;
  
}
Chris Strickland
  • 3,388
  • 1
  • 16
  • 18
0
  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];
    }
Steven Spungin
  • 27,002
  • 5
  • 88
  • 78