0

I'm looking for random counting without duplicate numbers I found something like this without comment random

        var randnums = [0,1,2,3,4,5,6];

        setInterval(function() {
            var m = Math.floor(Math.random()*randnums.length);
            $('ul li:nth-of-type('+(randnums[m])+')').animate({
                opacity:1
            },400)
            console.log(randnums[m])
        }, 300);

EXAMPLE: CODEPEN

What I want to accomplish:

  • when u check console log you will see that random isn't working as I think suppose to. I think random should work for example 4,3,5,1,6,2 with interval 300 for each number. Now it is working like every 300 ms choose numer 1,2,2,4,5,0 etc so after 4 sec you couldnt see for example 1.

  • second think I want to create script which count elements .lenght of elements (li) then create array and pick random numbers without duplicate.

I was looking for help all over net for long time with no positive result.

Szymon
  • 1,281
  • 1
  • 20
  • 36
  • 1
    A [dup](http://stackoverflow.com/questions/6625551/math-random-number-without-repeating-a-previous-number?rq=1)? – Teemu Nov 22 '13 at 20:36
  • I used that one and it doesnt work as you can see in example – Szymon Nov 22 '13 at 21:35
  • Looks like you've forgotten to splice the `randnums` array after use... – Teemu Nov 22 '13 at 23:27
  • Hmm, but splice does nothing in this script. Or I dont rly understand this. When I pust line with splice it show only one number from array. So please could you help me ?:) – Szymon Nov 23 '13 at 09:12

2 Answers2

1

Test this:

var randnums = [0, 1, 2, 3, 4, 5, 6],
    delay = setInterval(function () {
        var m = Math.floor(Math.random() * randnums.length);
        console.log(randnums[m]);
        randnums.splice(m,1);
        if (!randnums.length) {
            clearInterval(delay);
        }
    }, 300);

splice() is a very important part of this script, it "throws away" a used number. I've also added a check, which stops the interval, when all numbers are used.


EDIT

You can create an array of length of X with a for loop, for example:

var randnums = [], n, len = 12;
for (n = 0; n < len; n++) {
   randnums.push(n);
}
Teemu
  • 22,918
  • 7
  • 53
  • 106
  • That's anwser, indeed. But can you help me with second part? When i got more elements and for example 12 'li' how can I create array with number elements [0,1,2,...,11,12] when 13 elements [0,1,2,...,11,13] etc – Szymon Nov 23 '13 at 14:14
  • 1
    @SzymonDziewoński I've updated my answer. Btw. "OP" means Original Poster, i.e. you. – Teemu Nov 23 '13 at 14:32
  • Maybe I'm doin something wrong but when I change var randnums it show undefined in console.log and I supose it should return [0,1,2,...,11,12] – Szymon Nov 23 '13 at 14:44
  • @SzymonDziewoński That's my bad, there was a typo in my code. I've corrected it now. The code creates `[0, 1, 2, ... , 11]`, which has the length of 12. – Teemu Nov 23 '13 at 14:56
  • yes, now I see it is working. You save my day mate:). I really appreciate your time spent on this problem and THANK YOU very much :) I owe you one !:) – Szymon Nov 23 '13 at 14:57
0

An alternative to splicing, if you want to be able to reuse the array or loop the animation, try this:

var randnums = [0,1,2,3,4,5,6];
randnums.sort(function(a,b) {return Math.random()-0.5;}); // "shuffle" the array
setInterval(function() {
    var n;
    randnums.push(n = randnums.shift()); // cycle the first element to the back
    // do something with n
},300);
Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • I like the "shuffle first" approach better, **but** this shuffle function is really bad and will produce extremely un-evenly distributed results. You should use something like Fisher-Yates instead. – Ingo Bürk Nov 23 '13 at 10:39
  • That's why "shuffle" is in quotes :p It's random-ish, but that's what you get for trying to shuffle in a single line of code ;) – Niet the Dark Absol Nov 23 '13 at 10:54
  • Then you should make it clear to the OP who obviously wouldn't know this. Don't just feed him unreliable code. – Ingo Bürk Nov 23 '13 at 11:09
  • Well, does OP *need* "good" randomness? Especially in JavaScript, where nothing is to be trusted, I submit that "good enough" randomness is, indeed, good enough. – Niet the Dark Absol Nov 23 '13 at 11:56
  • Maybe he doesn't need it, maybe he does. My point is that by lying to him you take this decision from him and that makes it a bad answer. Also, you *know* how to improve the code, so why don't you just do it? It certainly wouldn't *hurt*, would it? And if you prefer to have the bad code in there, at least mention that it won't actually give an even distribution. – Ingo Bürk Nov 23 '13 at 12:08
  • I dont really undesrtand why you are fighting each other. WHats mean OP (overpowered in games) but here. It does work, yes thank but anwser @Teemu is better cuz it count only once. Thank you for your time I do really appreciate that:) – Szymon Nov 23 '13 at 14:14
  • OP means "Original Poster", so it refers to the person asking the question (you). – Ingo Bürk Nov 24 '13 at 15:35