Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-06-2008, 04:12 PM   PM User | #1
ScabbyDog
New Coder

 
Join Date: Oct 2008
Posts: 10
Thanks: 1
Thanked 0 Times in 0 Posts
ScabbyDog is an unknown quantity at this point
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;
}
ScabbyDog is offline   Reply With Quote
Old 10-06-2008, 05:26 PM   PM User | #2
vwphillips
Senior Coder

 
Join Date: Mar 2005
Location: Portsmouth UK
Posts: 4,354
Thanks: 3
Thanked 458 Times in 445 Posts
vwphillips is a jewel in the roughvwphillips is a jewel in the roughvwphillips is a jewel in the rough
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.uk/

If my post has been useful please donate to http://www.operationsmile.org.uk/
vwphillips is offline   Reply With Quote
Old 10-06-2008, 06:08 PM   PM User | #3
ScabbyDog
New Coder

 
Join Date: Oct 2008
Posts: 10
Thanks: 1
Thanked 0 Times in 0 Posts
ScabbyDog is an unknown quantity at this point
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 :-(
ScabbyDog is offline   Reply With Quote
Old 10-07-2008, 05:21 AM   PM User | #4
Trinithis
Regular Coder

 
Join Date: Jun 2007
Location: USA
Posts: 527
Thanks: 26
Thanked 74 Times in 72 Posts
Trinithis will become famous soon enough
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 is offline   Reply With Quote
Old 10-07-2008, 03:30 PM   PM User | #5
ScabbyDog
New Coder

 
Join Date: Oct 2008
Posts: 10
Thanks: 1
Thanked 0 Times in 0 Posts
ScabbyDog is an unknown quantity at this point
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!
ScabbyDog is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:14 AM.


Advertisement
Log in to turn off these ads.