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 04-15-2010, 04:27 AM   PM User | #1
001921
New to the CF scene

 
Join Date: Apr 2010
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
001921 is an unknown quantity at this point
java script random array shufler

hi I'm just new to java and I'm trying to write a script that will shuffle the information in a array with no gaps and no repeats. below is that code that i have so far. thanks ben
[CODE]
//constants
SWAPS_TO_BE_MADE = 30;
//variables
var myArray = new Array('Ace',2,3,4,5,6,7,8,9,10,'Jack','Queen','King'); // the array that entities will be shuffled from
var temp = 0;
var outPutArray = new Array (12);
var arrayLength = myArray.length;
var index = 0;
var swapIndex = 0;
var self_swaps = 0;
var counter = 0;

temp=myArray[swapIndex]

document.write('<h1>Array shuffling</h1>');
document.write('<p>Before shuffle: '+myArray+'</p>');

//loop
while(counter < SWAPS_TO_BE_MADE) {
index =Math.floor(Math.random()*arrayLength);
swapIndex =Math.floor(Math.random()*arrayLength);
var index_value = index;
var range = 1;
myArray.splice(index_value,range,'spilced');
outPutArray[swapIndex] = index;


if(index == swapIndex) {
self_swaps++;
}
counter++;
index = (index + 1 % arrayLength);
swapIndex= (swapIndex + 1 % arrayLength);
}

document.write('<p>After shuffle: '+outPutArray+'</p>');
document.write('<p>After shuffle: '+myArray+'</p>');
[ICODE]
001921 is offline   Reply With Quote
Old 04-15-2010, 06:05 AM   PM User | #2
mrhoo
Regular Coder

 
Join Date: Mar 2006
Posts: 708
Thanks: 30
Thanked 127 Times in 118 Posts
mrhoo will become famous soon enoughmrhoo will become famous soon enough
There's easier ways to shuffle an array
var myArray = ['Ace',2,3,4,5,6,7,8,9,10,'Jack','Queen','King'];
Code:
Array.prototype.shuffle= function(){
	return this.sort(function(){
		return 0.5 - Math.random();
	})
}
alert(myArray.shuffle())

Code:
Array.prototype.disorder= function(){
	var i, temp, L= this.length, A= this.concat();
	while(--L){
		i= Math.floor(Math.random()*L);
		temp= A[i];
		A[i]= A[L];
		A[L]= temp;
	}
	return A;
}
alert(myArray.disorder());
mrhoo is offline   Reply With Quote
Users who have thanked mrhoo for this post:
001921 (04-15-2010)
Old 04-15-2010, 06:13 AM   PM User | #3
001921
New to the CF scene

 
Join Date: Apr 2010
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
001921 is an unknown quantity at this point
thanks a lot that works much better thanks
001921 is offline   Reply With Quote
Old 04-15-2010, 07:20 AM   PM User | #4
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Or rather shorter:-

Code:
<script type = "text/javascript">
var imgArray = new Array('Ace',2,3,4,5,6,7,8,9,10,'Jack','Queen','King'); 
function randOrd(){return (Math.round(Math.random())-0.5); }
imgArray.sort(randOrd);
alert (imgArray);
<script>
Quote:
I'm just new to java
Java and Javascript are entirely different programming languages, in spite of the confusingly similar names.

Last edited by Philip M; 04-15-2010 at 09:23 AM..
Philip M is offline   Reply With Quote
Reply

Bookmarks

Tags
array, javascript, random, shuffle

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 02:36 PM.


Advertisement
Log in to turn off these ads.