1

I am a student who still needs to study a lot.

I want to make random arrangement between 1-12, and I have twelve contents, so I tried to give them numbers randomly (1-12).

so I tried this for random number:

function getRandom(min, max) {
    return Math.floor((Math.random() * (max - min + 1)) + min);
}
 
function getRandomArray(min, max, count) {
 
    if (max - min + 1 < count) return;
 
    var rdm = [];
    
    while (1) {
        var index = getRandom(min, max);
 
        if (rdm.indexOf(index) > -1) {
            continue;
        }
        rdm.push(index);

        if (rdm.length == count) {
            break;
        }
    }

    return rdm
}

and got random numbers (of 12)...

getRandomArray(1, 12, 12)

and give class name... but It wouldn't work the way I thought.

var contentNumber = [getRandomArray(1, 12, 12)];
$content.eq(0).addClass(contentNumber[0]);
$content.eq(1).addClass(contentNumber[1]);
...
$content.eq(11).addClass(contentNumber[11]);

I thought it could be an 'Array' but it was just a bunch of numbers. Can I make this an Array?

Or maybe I have to change my initial expectation of it...

Mangoo
  • 25
  • 4
  • 3
    You shouldn't put `getRandomArray(1, 12, 12)` inside `[]`. It's already returning an array, you don't need to put it inside another array. – Barmar Aug 17 '23 at 19:40
  • 2
    BTW, there's a much better way to make an array from 1-12 in random order. Make the array in order, then shuffle it. See https://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array – Barmar Aug 17 '23 at 19:41
  • 2
    Also don't use `while(1)`, at the very least use `while(true)` but even that's not what you want, you want `while(rdm.length < ...)` so that you stop once you have the number of random values you need. But as mentioned by @Barmar, this is not a good way to get a randomized array: generate an ordered array, and then shuffle it. – Mike 'Pomax' Kamermans Aug 17 '23 at 20:11
  • Does this answer your question? [How to randomize (shuffle) a JavaScript array?](https://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array) – Maarten Bodewes Aug 17 '23 at 21:03
  • welcome to stackoverflow, btw you can upvote answers (the arrow up) additionally to accepting – Alexander Nenashev Aug 18 '23 at 11:03

1 Answers1

0

As mentioned in comments you can generate an array of consecutive numbers (use index + 1) and shuffle it (sort by a random number):

const arr = Array
  // generate random number for sorting(shuffling) and consecutive numbers
  .from({length: 12}, (_, idx) => [Math.random(), idx + 1])
  // sort(shuffle) by the random number
  .sort(([a],[b]) => a - b)
  // map the sorted array into a suffled consecutive numbers array
  .map(([_, num]) => num);

console.log(...arr);
Alexander Nenashev
  • 8,775
  • 2
  • 6
  • 17
  • I think that the answer to this question should point out the programming mistake. Note that the title doesn't really ask for randomizing specifically, although that could be an A, B problem. However, if that would be an answer then it is a dupe, and we should point to the dupe (with many good answers) instead. – Maarten Bodewes Aug 17 '23 at 21:05