How to generate "eye candy" random 0/1 in javascript (contest)

130 Views Asked by At

I made an image gallery, that used two different animations, used in random order. But I realised, that my human feel is not satisfied.

js Math.floor(Math.random() * 2) generates sometimes 000000011111011111110000000

So I try to create some more "eye candy" random generator.

I know, it is hard to made 'real answer' to this. Feel free to post your answer and/or up/downvote others.

jsFiddle

2

There are 2 best solutions below

0
On

Oposite is better

for (j = 0; j < 100; j++) {
    var rand = (Math.random() < 0.8) ? Math.abs(rand - 1) : rand;
}

result: 0101010101010001010101010101010101010101101001001001010010101000101001010101010101101010101001010101

0
On

Max two times same

for (j = 0; j < 100; j++) {
    var oldrand,
    i,
    rand = Math.floor(Math.random() * 2);
    if (oldrand === rand) {
        if (++i === 2) {
            rand = Math.abs(oldrand - 1);
            i = 0;
        }

    } else {
        i = 0;
    }
    oldrand = rand;
}

result 0010110110010010010101001101010101001011001100100110110110011010011001101101101100110011001011010010