-1

I have a function to select a random number from 0 to 45 and then I show the div with the specific ID. It's working fine but it repeats a number.

Can anyone advise so it won't repeat numbers?

I call the function onclick like this

$(".skip").click(function () {

    scared++;
    $("#counter").html("My current count is: " + dared);
    var d = 50;


    /*$(".question").addClass("hideMe");
    $(this).parents("div").next("div").removeClass("hideMe");*/
    var r = Math.round(Math.random() * 44) + 1;
    var newquestion = "q" + r;
    $('.active').removeClass("active");
    $("#" + newquestion).addClass("active");


    if (scared > 44) {
        $('.main').fadeOut('fast');

        $('.logo').switchClass("logo", "share");
        $('.progress').css("display", "none");
        $('.share-game').css("display", "block");
        $('.hero').css("right", "-240px");
        $('#score-total').html(score + '');
    } else {

    }

    $('.red-line').append('<div id="children' + (d++) + '" class="red"></div>');

    return false;
});
Spokey
  • 10,974
  • 2
  • 28
  • 44
  • 2
    add your used random number to an array, and check if this random number exists in that array, and do it while not in a loop. – vaso123 Nov 13 '14 at 14:50
  • Where does `dared` at line 3 come from? – Roamer-1888 Nov 13 '14 at 14:53
  • Are you looking for the random number to not be repeated back-to-back? Or for that random number to never be used again? – Drew Kennedy Nov 13 '14 at 14:56
  • Reading between the lines, it sounds rather like you want to create and array then [shuffle it](http://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array). – Roamer-1888 Nov 13 '14 at 15:40
  • I have a quiz with 45 questions, I need to show every question one time but not repeat anyone, the dared is the points I give every time they press click, if the user pres skeep i need to show next div asweel without repeat until he run the 45 questions, hope this help – user3415054 Nov 13 '14 at 16:34

2 Answers2

2

You can see what i did.

var usedNumbers = [];
var randomNumbers = [];
$(function() {

    //Getting 20 random numbers
    for (i = 0; i < 20; i++) {
        randomNumbers.push(getRandomNumber());
    }
    console.log(randomNumbers);

    function getRandomNumber() {
        var hasInArray = true;
        do {
            var r = Math.round(Math.random() * 44) + 1;
            if (usedNumbers.indexOf(r) === -1) {
                usedNumbers.push(r);
                hasInArray = false;
                return r;
            }
        } while (hasInArray === true);

    }
});

Warning to not set the numbers of randomnumbers more then what you want to get, because that will cause an infinite loop!

vaso123
  • 12,347
  • 4
  • 34
  • 64
0

Use an array to capture the used numbers and then check that array on each click, generating a new number if that one is found. It resets back to an empty array when it is full.

var savedNumbers = [];

function getRandom() {
  if (savedNumbers.length === 45) {
    savedNumbers = [];
  }
  var r = Math.round(Math.random() * 44) + 1;
  if (savedNumbers.indexOf(r) > -1) {
    getRandom();
  } else {
    savedNumbers.push(r);
    return r;
  }
}

DEMO

Andy
  • 61,948
  • 13
  • 68
  • 95
  • getRandom function I need to be able to run it 45 times – user3415054 Nov 13 '14 at 16:29
  • Look at the demo. You run `getRandom` every time you click the button which is what you want to do, isn't it? – Andy Nov 13 '14 at 17:21
  • if (savedNumbers.indexOf(r) > -1) { getRandom(); } this line run the function again but does not show the div until it runs again all function. – user3415054 Nov 18 '14 at 09:31