JavaScript generate random numbers without repeating

后端 未结 4 1289
天涯浪人
天涯浪人 2021-01-03 11:04

I have some code where I have an array of x amount of items. In this case, videos, and I want to randomly call a video, however if the current video already called is the sa

4条回答
  •  时光取名叫无心
    2021-01-03 11:26

    randojs.com makes this simple:

    var sequence = randoSequence(videoLinks);
    

    Then, you can loop through videoLinks to get the values in random order. Here's how you get the first random value:

    sequence[0].value
    

    When you run out, just generate a new sequence if needed.

    You just need to add the following to the head of your html document, and you can do pretty much whatever you want with randomness easily. Random numbers, random values from arrays, random jquery elements, random properties from objects, and even preventing repetitions as I've shown here.

    
    

提交回复
热议问题