[array_shuffle]

Description

Link: [array_shuffle]
Author: Jason Huck
Category: Array
Version: 8.x
License: Public Domain
Posted: Oct. 24, 2005
Updated: Oct. 24, 2005
More by this author...
Returns the given array in a new, random order.

Parameters

none


Sample Usage

var('orderedItems') = array(1,2,3,4,5);
var('shuffledItems') = array_shuffle($orderedItems);
$orderedItems;

// -> array: (3),(2),(1),(5),(4)
						

Source Code

Click the "Download" button below to retrieve a copy of this tag, including the complete documentation and sample usage shown on this page. Place the downloaded ".inc" file in your LassoStartup folder, restart Lasso, and you can begin using this tag immediately.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
define_tag(
	'shuffle',
	-namespace='array_',
	-priority='replace',
	-description='Returns the given array in a new, random order.'
);
	fail_if(
		!params->size || !params->first->isa('array'),
		-1,
		'[array_shuffle] requires an array as a parameter.'
	);
	
	local(
		'in' = params->first,
		'out' = array
	);
	
	#out->reserve(#in->size);
	
	while(#in->size);
		local('x') = math_random(
			-lower=1, 
			-upper=#in->size
		);

		#out->insert(#in->get(#x));	
		#in->remove(#x);
	/while;
	
	return(#out);
/define_tag;

 

Related Tags



Comments

none

Email:


Password:



Newest

Most Popular