Flash Website Builder- Trendy Site Builder is a Flash Site Building tool that helps users build stunning websites. Check Out Custom Custom Logo Design by LogoBee. Website Design and Free Logo Templates available.
 CodingForums.com Non repeating Random Number Generator

Before you post, read our: Rules & Posting Guidelines

Enjoy an ad free experience by logging in. Not a member yet? Register.
 10-06-2008, 05:12 PM PM User | #1 ScabbyDog 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; }
 10-06-2008, 06:26 PM PM User | #2 vwphillips Senior Coder   Join Date: Mar 2005 Location: Portsmouth UK Posts: 4,431 Thanks: 3 Thanked 481 Times in 468 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.uk/ If my post has been useful please donate to http://www.operationsmile.org.uk/
10-06-2008, 07:08 PM   PM User | #3
ScabbyDog
New Coder

Join Date: Oct 2008
Posts: 10
Thanks: 1
Thanked 0 Times in 0 Posts

Quote:
 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 :-(

 10-07-2008, 06:21 AM PM User | #4 Trinithis 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
10-07-2008, 04:30 PM   PM User | #5
ScabbyDog
New Coder

Join Date: Oct 2008
Posts: 10
Thanks: 1
Thanked 0 Times in 0 Posts
Quote:
 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!

 Bookmarks

 Thread Tools Rate This Thread Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 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 Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home :: Client side development     JavaScript programming         DOM and JSON scripting         Ajax and Design         JavaScript frameworks         Post a JavaScript     HTML & CSS     XML     Flash & ActionScript         Adobe Flex     Graphics and Multimedia discussions     General web building         Site reviews         Building for mobile devices :: Server side development     Apache configuration     Perl/ CGI     PHP         Post a PHP snippet     MySQL         Other Databases     Ruby & Ruby On Rails     ASP     ASP.NET     Java and JSP     Other server side languages/ issues         ColdFusion         Python :: Computing & Sciences     Computer Programming     Computer/PC discussions     Geek News and Humour Web Projects and Services Marketplace     Web Projects         Small projects (quick fixes and changes)         Medium projects (new script, new features, etc)         Large Projects (new web application, complex features etc)         Unknown sized projects (request quote)         Vacant job positions         Looking for work/ for hire         Project collaboration/ partnership         Paid work offers and requests (Now CLOSED)     Career, job, and business ideas or advice     Domains, Sites, and Designs for sale         Domains for sale         Websites for sale         Design templates and graphics for sale :: Other forums     Member Offers     Forum feedback and announcements

All times are GMT +1. The time now is 08:26 AM.