PDA

View Full Version : Non repeating Random Number Generator

ScabbyDog
10-06-2008, 04:12 PM
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;
}

vwphillips
10-06-2008, 05:26 PM
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;
}

ScabbyDog
10-06-2008, 06:08 PM
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 :-(

Trinithis
10-07-2008, 05:21 AM
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();

ScabbyDog
10-07-2008, 03:30 PM
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!