#Random inside random

14 messages · Page 1 of 1 (latest)

delicate willow
#

Hi, how to add a random variation inside of a random variation?
The comand below triggers videos, but 4.1, 4.2 and 4.3 are the same video, but slightly different, so whithin this comman the % is not even, 4.1-4.3 will be triggered more often then the othrs
Can I put it in a container maybe so once executed and in case of picking up video 4 it will then to the second randomizer and pick the video?

The video q-ty is always changing, so I was expecting to add something like in case the first randomizer hits video 4 it will then launch second randomizer for the videos tagged with number 4

               "video":     [
 
"video 1",
"video 2",
"video 3",
"video 4.1",
"video 4.2",
"video 4.3",
           
              
            ] }, ```

https://pastebin.com/NMt9aq8P
oak bone
#

Math.random is not really random. A better result of randomness can be achieved this way for example:

const shuffle = (array) => {
        for (let i = array.length - 1; i > 0; i--) {
            const j = Math.floor(Math.random() * (i + 1));
            [ array[ i ], array[ j ] ] = [ array[ j ], array[ i ] ];
        }
        // pick first item of shuffled array
        return array[ 0 ];
 };
delicate willow
#

So do I need to replace this:

         showThis = showThis[Math.floor(Math.random() * showThis.length)];   ```

with this?

``` if(Array.isArray(showThis))
    showThis = (array) => {
        for (let i = array.length - 1; i > 0; i--) {
            showThis j = Math.floor(Math.random() * (i + 1));
            [ array[ i ], array[ j ] ] = [ array[ j ], array[ i ] ];
        }
        // pick first item of shuffled array
        return array[ 0 ];
 };```

*I'm not into programming, yes *
oak bone
#

you can have that shuffle function outside (as arrow function it must be declared BEFORE you can use it)

and then use it in your code. OR you can place the check directly in the function and just call it

#
function shuffle(array) {
        if(!Array.isArray(array) return; // don't even read the rest of code here
        for (let i = array.length - 1; i > 0; i--) {
            const j = Math.floor(Math.random() * (i + 1));
            [ array[ i ], array[ j ] ] = [ array[ j ], array[ i ] ];
        }
        // pick first item of shuffled array
        return array[ 0 ];
 };

and then use it like

const myRandomItem = shuffle(showThis);
delicate willow
oak bone
#

I would spread some console.logs to see where it breaks.

delicate willow
#

I don't think there is a console there
It's a streamelements overylay\

delicate willow
#

btw is there really a benefit of such shuffle? since I'm not playing videos in sequence and each time command triggers randomizer is a command to trigger just 1 video and then reset

#

Also concerning the issue with having 20 videos in total which 10 are unique videos and the other 10 are just variation of one video, how to make script work in a way that those 10 videos would be treated as 1 video, so total of 11 video, but in case randomizer hits that "1" video consisting of 10 variations it will then launch another randomizer to pick a video among those 10? This was the main issue of my thread

#

How will it work in case, like I have 50 videos and then inside there will be 5 videos with each of 5 variations, so 25 + 5 + 5 + 5 + 5+ 5 (30 unique vids in total)
What will be the best solution here? Will the alert command change, the one who consist of those 50 video links together with the main code for arrays?

Like those 50 vids are used in command !randomdance
But they also might be in a command !random, etc

oak bone
#

that could be a nested array where you pick a random array then inside a random item.

Actually the array I shuffle with that is just an array of numbers, not the content itself.

oak bone
#

If your fine with the degree of randomness of Math.random() you can do this:
(This also does NOT mutate the initial array)

const nestedArray = [
    ["a", "b"],
    ["c", ["d", "e"]],
    "f",
    ["g", ["h", "i"]]
];

const getRandomIndex = (max) => Math.floor(Math.random() * max);

function getRandomElement(array) {
    if (Array.isArray(array) && array.length > 0) {
        const randomIndex = getRandomIndex(array.length);
        const selectedElement = array[randomIndex];

        if (Array.isArray(selectedElement)) {// check wether the chosen item on current level is array and recurse
            return getRandomElement(selectedElement);
        } else {
            return selectedElement;
        }
    } else {
        return undefined;
    }
}

const randomElement = getRandomElement(nestedArray);
console.log(randomElement);