Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Non repeating Random Number Generator

1. ## 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??....
-------------------

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

• 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;
}```

• Originally Posted by vwphillips
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
at the very top of the code.

It still gives me the same results :-(

• 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();```

• Originally Posted by Trinithis
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
•