0

The Idea is: click a button to replace a headline with a unique string of an array. The problem with this is that I've used a string of the array before like this:

headlines = new Array("Good", "Bad", "Ugly", "Random Headline");
var randomNumberBefore = 4;
alert (headlines[randomNumberBefore]);

but I dont want to display the same headline again, thatswhy it is key check that the actual index randomNumberBefore is not the same number like new index randomNumber. The following function makes sense to me, but sometimes it returns the same number, that causes the headline is replaced by itself and the user noticed no change.

function randomNumberByRange (range, number) {
    var r;
    do {
        var r = Math.floor(Math.random() * range);
    } while (r == number);
    return r;
}

$(document).on('click','.nextquote' , function() {
    var randomNumber = randomNumberByRange( headlines.length, randomNumberBefore);
    var nextHeadline = headlines[randomNumber];

    $(".bannertext").text(nextHeadline);
    console.log(nextHeadline);

});

Any Ideas to get unique headlines per click?

Here is my starting fiddle.

--

Here is the final fiddle.

p2or
  • 339
  • 8
  • 27
  • 3
    And when all items have been shown already, show what? – dfsq Oct 28 '14 at 18:32
  • Dang, that's the most random 4 I've ever seen – Sterling Archer Oct 28 '14 at 18:34
  • Google for "(Knuth) Fisher Yates shuffle", then just show 'em in order. – Alnitak Oct 28 '14 at 18:36
  • Is the purpose to show 1-N items in random order, or simply to avoid showing the same item twice in a row? For example, if it goes `Good>Bad>Good`, is that acceptable? – StriplingWarrior Oct 28 '14 at 18:58
  • @Alnitak He clearly says: "The following function makes sense to me, but sometimes it returns the same number, that causes the headline is replaced by itself and the user noticed no change." It has nothing to do with the duplicate you posted! – Dinu Sorin Oct 28 '14 at 19:24
  • @DinuSorin yes, it does, because the correct implementation it so shuffle the array and then show them in shuffled order. – Alnitak Oct 28 '14 at 22:13
  • 1
    @Alnitak that is the correct implementation if you want a random permutation of an array. He wanted an infinite series of headlines where no two consecutive headlines are the same (+ balanced occurrence). Two different problems, two different algorithms to solve them. – Dinu Sorin Oct 29 '14 at 09:27
  • @dfsq - good question. Shuffle all items again. – p2or Oct 29 '14 at 09:53
  • @Alnitak - We cant merge the answers to the other question, thatswhy it cant be a duplicate. Ive also edited the question for you to be clearly different. – p2or Oct 29 '14 at 10:15

2 Answers2

2

You forgot to assign the old value to randomNumberBefore; after

var randomNumber = randomNumberByRange( headlines.length, randomNumberBefore);

put

randomNumberBefore = randomNumber;

PS: There is a way to make the randomNumberByRange function more performant:

function randomNumberByRange (range, number) {
    var r = Math.floor(Math.random() * (range-1));
    if(r >= number)r++;
    return r;
}

but, if you have many headlines, your function is good enough (as collision probability drops with the number of items you have in the list)

Dinu Sorin
  • 215
  • 2
  • 6
1

If you don't want repetition, simply remove the used elements from the array.

var h = new Array("Good", "Bad", "Ugly", "Random Headline");
while (h.length > 0) {
    var i = Math.floor(Math.random() * h.length);
    var t = h.splice(i, 1);
    console.log(t);
}
Etheryte
  • 24,589
  • 11
  • 71
  • 116