0

I couldn't find any results for this question. It may be very simple but I can't do it.

I need to generate a random number when I click the screen so I can print an image like "jt_number.jpg". The problem is that I have 24 photos and it is very often that I get the image repeated 2 or 3 times in a row. I need it to not repeat with the previous or next number I get.

This is my code to get a random number:

var numbers = []; // new empty array
        
        var min, max, r, n, p;
        min = 0;
        max = 31;
        r = 1; // how many numbers you want to extract

        
        for (let i = 0; i < r; i++) {
            do {
                n = Math.floor(Math.random() * (max - min + 1)) + min;
                p = numbers.includes(n);
                if(!p){
                    numbers.push(n);
                }
            }
            while(p);
        }
        
        console.log(numbers.join(" - "));

Then I inject the number here:

var obj, source;
        obj = document.createElement('img');
        $(obj).attr('id', 'jt_' + numbers );
        $(obj).attr('src', 'coming/jt_' + numbers + '.jpg');
        $(obj).addClass('active');
        $(obj).css('display', 'block');
        
        $("#wrapper").append(obj);
        $(obj).append(source);

        var xposition = (event.clientX - obj.offsetLeft - obj.offsetWidth/2);
        var yposition = (event.clientY - obj.offsetTop - obj.offsetHeight/2);
        obj.style.transform = "translate("+ xposition + "px," + yposition + "px)";

Any ideas? Thank you.

0 Answers0