[ create a new paste ] login | about

Link: http://codepad.org/sBMEsXJ1    [ raw code | output | fork ]

PHP, pasted on Jun 24:
<?php

	$array = array('abc', 'def', 'xyz', 'qqq');
	
	$numRandoms = 3;
	
	$final = array();
	
	$count = count($array);
	
	if ($count >= $numRandoms) {
	
		while (count($final) < $numRandoms) {
			
			$random = $array[rand(0, $count - 1)];
			
			if (!in_array($random, $final)) {
				
				array_push($final, $random);
			}
		}
	}
	
	var_dump($final);

?>


Output:
1
2
3
4
5
6
7
8
array(3) {
  [0]=>
  string(3) "def"
  [1]=>
  string(3) "qqq"
  [2]=>
  string(3) "abc"
}


Create a new paste based on this one


Comments: