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.

9
  • 3
    And when all items have been shown already, show what? Commented Oct 28, 2014 at 18:32
  • Dang, that's the most random 4 I've ever seen Commented Oct 28, 2014 at 18:34
  • Google for "(Knuth) Fisher Yates shuffle", then just show 'em in order. Commented Oct 28, 2014 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? Commented Oct 28, 2014 at 18:58
  • 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. Commented Oct 29, 2014 at 9:27

2 Answers 2

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)

Sign up to request clarification or add additional context in comments.

Comments

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);
}

2 Comments

note to OP - if it's not obvious, this code will destroy the original array
Simple and beautifull :) - Thanks Nit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.