0

I would really appreciate some help with getting this random card generator working. The code I'm currently using can be found below, and wasn't written by me (but was provided to use publicly). I know very little about Javascript..

I have a total of 49 cards in a database / collection on my website, and this basically picks 3 of them to display and sets the rest to hidden. However, I have noticed after around 50-100 refreshes that it seems to be picking certain cards more often than others, and too many times for it to be a coincidence. Specifically those at the start of the list in the database.. (Feminine, Masculine, Urgency)

You can see the live example here: https://cosmic-runes.webflow.io/

Not sure how complex of a problem this is to solve, or if it's just a small tweak..

  $(document).ready(function() {
        var show_limit = 3;
    $('.card-flip-item').each(function(){
      if (   $(this).index() >= show_limit) {
         $(this).addClass('hidden-list-item');

         }
      });
    });

    var cards = $(".card-flip-item");
    for(var i = 0; i < cards.length; i++){
        var target = Math.floor(Math.random() * cards.length -1) + 1;
        var target2 = Math.floor(Math.random() * cards.length -1) +1;
        cards.eq(target).before(cards.eq(target2));
    }
  • `Math.random()` generates a pseudo-random (appears to be random but isn't truly so) number. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random – phuzi May 13 '21 at 11:30
  • Does this answer your question? [How does Math.random() work in javascript?](https://stackoverflow.com/questions/20109006/how-does-math-random-work-in-javascript) – phuzi May 13 '21 at 11:33
  • Thanks @phuzi, the link is definitely useful in understanding this morer. I'm not sure exactly what you're saying though or which bit I need to edit above to make this work better than it is currently. – JMillion May 13 '21 at 16:26
  • For more/cryptographically random values use the `window.crypto.getRandomValues()` – phuzi May 13 '21 at 18:34

0 Answers0