Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Apr 2010
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    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]

  • #2
    Regular Coder
    Join Date
    Mar 2006
    Posts
    728
    Thanks
    35
    Thanked 132 Times in 123 Posts
    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());

  • Users who have thanked mrhoo for this post:

    001921 (04-15-2010)

  • #3
    New to the CF scene
    Join Date
    Apr 2010
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts
    thanks a lot that works much better thanks

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,310
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    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>
    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 10:23 AM.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •