Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 523
  • Last Modified:

JavaScript: Randomize Array Order / Key

How can I randomize the order and key numbers for my array?
var fadeimages=new Array()
	fadeimages[0]=["/images/home/i1.jpg", "", ""];
	fadeimages[1]=["/images/home/i2.jpg", "", ""];
	fadeimages[2]=["/images/home/i3.jpg", "", ""];
	fadeimages[3]=["/images/home/i4.jpg", "", ""];
	fadeimages[4]=["/images/home/i5.jpg", "", ""];
	fadeimages[5]=["/images/home/i6.jpg", "", ""];
	fadeimages[6]=["/images/home/i7.jpg", "", ""];
	fadeimages[7]=["/images/home/i8.jpg", "", ""];

Open in new window

0
hankknight
Asked:
hankknight
  • 5
  • 2
2 Solutions
 
leakim971PluritechnicianCommented:
Hello hankknight,

Random the way you get one.

a = fadeimages[ Math.random() * (fadeimages.length-1) ];

Regards
0
 
leakim971PluritechnicianCommented:
Correction (we need Integer :)

a = fadeimages[ parseInt( Math.random() * (fadeimages.length-1) ) ];

Open in new window

0
 
hankknightAuthor Commented:
Thanks, but I don't understand how to implement your idea.  I want to change the order of items in the array.
var fadeimages=new Array()
	//SET IMAGE PATHS. Extend or contract array as needed
	fadeimages[0]=["/images/home/i1.jpg", "", ""];
	fadeimages[1]=["/images/home/i2.jpg", "", ""];
	fadeimages[2]=["/images/home/i3.jpg", "", ""];
	fadeimages[3]=["/images/home/i4.jpg", "", ""];
	fadeimages[4]=["/images/home/i5.jpg", "", ""];
	fadeimages[5]=["/images/home/i6.jpg", "", ""];
	fadeimages[6]=["/images/home/i7.jpg", "", ""];
	fadeimages[7]=["/images/home/i8.jpg", "", ""];
	fadeimages = fadeimages[ parseInt( Math.random() * (fadeimages.length-1) ) ];
	alert(fadeimages[1]);

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
leakim971PluritechnicianCommented:
Like this :


var fadeimages=new Array()
        //SET IMAGE PATHS. Extend or contract array as needed
        fadeimages[0]=["/images/home/i1.jpg", "", ""];
        fadeimages[1]=["/images/home/i2.jpg", "", ""];
        fadeimages[2]=["/images/home/i3.jpg", "", ""];
        fadeimages[3]=["/images/home/i4.jpg", "", ""];
        fadeimages[4]=["/images/home/i5.jpg", "", ""];
        fadeimages[5]=["/images/home/i6.jpg", "", ""];
        fadeimages[6]=["/images/home/i7.jpg", "", ""];
        fadeimages[7]=["/images/home/i8.jpg", "", ""];
        alert(fadeimages[ parseInt( Math.random() * (fadeimages.length-1) ) ]);

Open in new window

0
 
hieloCommented:
>>Thanks, but I don't understand how to implement your idea
That's because he misunderstood you question. Here you go:
function randomize(arr)
{
	for (var i=0,limit=arr.length-1;i<=limit;i++)
	{
		var ri=Math.floor(Math.random()*(arr.length-i))+i;
		var temp=arr[ri];
		arr[ri]=arr[i];
		arr[i]=temp;
	}
return arr;	   
}

		var fadeimages=new Array()
        fadeimages[0]=["/images/home/i1.jpg", "", ""];
        fadeimages[1]=["/images/home/i2.jpg", "", ""];
        fadeimages[2]=["/images/home/i3.jpg", "", ""];
        fadeimages[3]=["/images/home/i4.jpg", "", ""];
        fadeimages[4]=["/images/home/i5.jpg", "", ""];
        fadeimages[5]=["/images/home/i6.jpg", "", ""];
        fadeimages[6]=["/images/home/i7.jpg", "", ""];
        fadeimages[7]=["/images/home/i8.jpg", "", ""];

randomize(fadeimages);

Open in new window

0
 
leakim971PluritechnicianCommented:
Hello hielo,

random the array content = random how to choose one item in the array

No ?

Regards.
0
 
hieloCommented:
>>random the array content = random how to choose one item in the array
>>No ?
No. I gave you the benefit of the doubt. I thought you misunderstood the question, but clearly you have an "implementation bug" :)

if you do:
alert(fadeimages[ parseInt( Math.random() * (fadeimages.length-1) ) ]);

back to back, you run the risk of getting the same random number consecutively. To clarify, if you do:
alert(fadeimages[ parseInt( Math.random() * (fadeimages.length-1) ) ]);

8 times consecutively, there is a high probability that you will see repeated numbers. For the array to be truly randomized, you need to randomly create  8 (in this case) unique numbers from 0...7. then use these random numbers to rearrange the indices in the original array.
Make sense?
0
 
leakim971PluritechnicianCommented:
>8 times consecutively
>Make sense?
Yes, in this case you're right! Thanks for your time.

@hankknight
Thanks for the points!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now