Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Oct 2008
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Non repeating Random Number Generator

    My first post here guys

    Basically I'm wondering what I can do to avoid a randomly generated number being repeated in the same column.
    This is the code I'm using....I'm trying to generate 3 random numbers between 1 and 10 without using the same number twice.

    Help??....
    Thanks in advance!
    -------------------

    window.onload = newCard;

    function newCard()
    {
    for (var i=0; i<1; i++)
    {
    var newNum = Math.floor(Math.random() * 9) + 1;
    document.getElementById("square" + i).innerHTML = newNum;
    }

    for (var i=9; i<10; i++)
    {
    var newNum = Math.floor(Math.random() * 9) + 1;
    document.getElementById("square" + i).innerHTML = newNum;
    }

    for (var i=18; i<19; i++)
    {
    var newNum = Math.floor(Math.random() * 9) + 1;
    document.getElementById("square" + i).innerHTML = newNum;
    }

  • #2
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,522
    Thanks
    3
    Thanked 508 Times in 495 Posts
    Code:
    var Nus=[-1,-1,-1];
    function newCard()
    {
    for (var i=0; i<1; i++)
    {
    var newNum = Math.floor(Math.random() * 9) + 1;
    while (newNum==Nus[0]) newNum = Math.floor(Math.random() * 9) + 1;
    Nus[0]=newNum;
    document.getElementById("square" + i).innerHTML = newNum;
    }
    
    for (var i=9; i<10; i++)
    {
    var newNum = Math.floor(Math.random() * 9) + 1;
    while (newNum==Nus[1]) newNum = Math.floor(Math.random() * 9) + 1;
    Nus[1]=newNum;
    document.getElementById("square" + i).innerHTML = newNum;
    }
    
    for (var i=18; i<19; i++)
    {
    var newNum = Math.floor(Math.random() * 9) + 1;
    while (newNum==Nus[2]) newNum = Math.floor(Math.random() * 9) + 1;
    Nus[2]=newNum;
    document.getElementById("square" + i).innerHTML = newNum;
    }
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/

  • #3
    New Coder
    Join Date
    Oct 2008
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Unhappy

    Quote Originally Posted by vwphillips View Post
    Code:
    var Nus=[-1,-1,-1];
    function newCard()
    {
    for (var i=0; i<1; i++)
    {
    var newNum = Math.floor(Math.random() * 9) + 1;
    while (newNum==Nus[0]) newNum = Math.floor(Math.random() * 9) + 1;
    Nus[0]=newNum;
    document.getElementById("square" + i).innerHTML = newNum;
    }
    
    for (var i=9; i<10; i++)
    {
    var newNum = Math.floor(Math.random() * 9) + 1;
    while (newNum==Nus[1]) newNum = Math.floor(Math.random() * 9) + 1;
    Nus[1]=newNum;
    document.getElementById("square" + i).innerHTML = newNum;
    }
    
    for (var i=18; i<19; i++)
    {
    var newNum = Math.floor(Math.random() * 9) + 1;
    while (newNum==Nus[2]) newNum = Math.floor(Math.random() * 9) + 1;
    Nus[2]=newNum;
    document.getElementById("square" + i).innerHTML = newNum;
    }
    I replaced my text with what you wrote above and left the
    window.onload = newCard;
    at the very top of the code.

    It still gives me the same results :-(

  • #4
    Regular Coder
    Join Date
    Jun 2007
    Location
    USA
    Posts
    527
    Thanks
    26
    Thanked 74 Times in 72 Posts
    Code:
    Array.fromRange = function(start, end) {
      var arr = [];
      while(start <= end)
        arr.push(start++);
    };
    
    Array.prototype.shuffle = function() {
      for(var i = this.length; i > 1; --i)
        this.swap(i - 1, Math.floor(Math.random() * i));
      return this;
    };
    
    Array.prototype.swap = function(i, j) {
      var t = this[i];
      this[i] = this[j]
      this[j] = t;
    };
    
    //////////////////////////////////////
    
    var rands = Array.fromRange(1, 10).shuffle();
    
    var num1 = rands.pop();
    var num2 = rands.pop();
    var num3 = rands.pop();
    Trinithis

  • #5
    New Coder
    Join Date
    Oct 2008
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Trinithis View Post
    Code:
    Array.fromRange = function(start, end) {
      var arr = [];
      while(start <= end)
        arr.push(start++);
    };
    
    Array.prototype.shuffle = function() {
      for(var i = this.length; i > 1; --i)
        this.swap(i - 1, Math.floor(Math.random() * i));
      return this;
    };
    
    Array.prototype.swap = function(i, j) {
      var t = this[i];
      this[i] = this[j]
      this[j] = t;
    };
    
    //////////////////////////////////////
    
    var rands = Array.fromRange(1, 10).shuffle();
    
    var num1 = rands.pop();
    var num2 = rands.pop();
    var num3 = rands.pop();
    sorry if i'm being stupid...but i'm a relatively new java programmer and learning slowly. could you please explain what this does as it doesn't work for me when i replace my code with this....just comes up blank again!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •