I try to create a variable who send a random value !
$random = mt_rand(5,10);
but i want the variable random with the number i choose like this:
$random = 5;10;15;20;25;30
The variable must send randomly 5 or 10 or 15 or 25 or 30 only
What you could do is using the mt_rand function with values from 1 to 6 then multiply them by 5.
Example:
$random = mt_rand(1, 6) * 5;
Like that?
$random = mt_rand(1,6) * 5;
Use array_rand to pick random key from array then you can put it into an another.
http://php.net/manual/en/function.array-rand.php
$input = array(5,10,15,20,25);
$rand_key = array_rand($input);
$another_array[] = $input[$rand_key];
You could use and array of element and get a random item
$items = Array(5,10,15,20,...50);
$myvalue = array_rand($items);
Assuming that you already have the values you want inside an array you can simply get a random position of the array. A rand() limited by the first element to the size of the array can solve this
<?php
$valores = [1,5,12,17,22,30,90,2,4,6]; // choose de values here
for($i=0;$i<200;$i++){ // just to print more than 1 value
echo $valores[rand(1,sizeof($valores))-1]."<br>"; // really pick the values here. start in 1 because we'll remove 1
}
Will it always be steps of 5? Then you could do something like
$random = mt_rand(1,6) * 5;
Otherwise I'd put the numbers that you want to choose from in an array and select a random element using mt_rand():
$random_temp = array(3,6,8,13,16,19);
$random = $random_temp[mt_rand(0,6)];
If the random numbers arrayed, then this may help:
$list=array(5,10,15,20,25,30);
$n=count($list)-1;
$random=$list(rand(0,$n));
$values = [5,10,15,20,25,30];
//shuffle the array
shuffle($values);
foreach($values as $v){
print $v;
}
always in different order...
You can use rand():
$random = rand(1, 6) * 5;
Will produce:
5, 10, 15, 25 or 30 randomly.
Please try this code
$random = rand(0,30);
if(($random%5)==0)
{
echo $random;
}
else
{
echo roundUpToAny($random);
}
function roundUpToAny($n,$x=5) {
return round(($n+$x/2)/$x)*$x;
}
Related
I am struggling to get the lowest value based on the answer of the client.
$client_answer = 28;
$array = array(10,20,30,40,50);
The answer that should be given is: 20
So every answer should be rounded down to the lower number.
Other examples:
$client_answer = 37;
$array = array(10,20,30,40,50);
answer should be 30.
$client_answer = 14;
$array = array(10,20,30,40,50);
answer should be 10.
$client_answer = 45;
$array = array(10,20,30,40,50);
answer should be 40.
Is there a php function that I can use for this?
If not how can this be accomplished?
You can filter the array to only contain values equal to or below the given value $client_answer, then use max() on the filtered array.
$value = max(array_filter($array, function($v) use ($client_answer) {
return $v <= $client_answer;
}));
Live demo at https://3v4l.org/KHB8r
This might be a very stupid answer, but in this specific case, you are trying to truncate the unit number? Why not try this:
$client_answer = intdiv( $client_answer, 10) * 10;
Divide by 10, get rid of the last digit, and multiply again.
EDIT : typo
Only this rounds correctly
$client_answer = 28;
$array = array(10,20,30,40,50);
rsort($array,1);
$min_diff = max($array);
$closest_val = max($array);
foreach($array as $val)
{
if(abs($client_answer - $val) < $min_diff)
{
$min_diff = abs($client_answer - $val);
$closest_val = $val;
}
}
echo $closest_val;
I know that rand(1,10); would generate a random number between 1 and 30.
How would I go about writing code that would pick a random number out of a group of numbers say 1,7,8 and 9?
Is it possible?
I am pretty sure rand is set up to only generate numbers within a range?
First thing is that rand(1,10) will generate a number between1 to 10 not 1 to 30.
PHP RAND FUNCTION
From a group of numbers, you can generate like this
$numbers = [1,7,8,9];
echo $numbers[rand(0,3)]
You can create custom array which you required, shuffle and get first value
$temp = [1,7,8,9];
shuffle($temp);
echo $temp[0];
shuffle — Shuffle an array
Demo
You could put your group of numbers into an array, and then use array_rand or shuffle to select one of them. For example:
$nums = array(1, 7, 8, 9);
$key = array_rand($nums);
echo $nums[$key] . PHP_EOL;
shuffle($nums);
echo $nums[0] . PHP_EOL;
Demo on 3v4l.org
Use this function (or inline code)
function SelectElementByRandom ( $list )
{
return count($list) ? $list[ rand(0,count($list)-1) ] : NULL;
}
The ?: operator ensures an result (NULL), even if the list is empty.
And for your example:
$result = SelectElementByRandom(array(1,7,8,9));
I would approach it like this. Create a loop that fills an array with random numbers in the range you're looking for (your group of numbers). After that, you choose a random index in that array, which is a random number in your group of numbers.
$min = 0;
$max = 10;
$group_size = 5;
$rand_group = array();
for($i = 0; $i < $group_size; $i++) {
$rand_group[$i] = rand($min,$max);
}
echo $rand_group[rand($min,$max)];
I have an array with multiple elements. I want to keep only the 10 most recent values. So I am reversing the array in a loop, checking if the element is within the first 10 range and if not, I unset the element from the array.
Only problem is that the unset does not work. I am using the key to unset the element, but somehow this does not work. The array keeps on growing. Any ideas?
$currentitem = rand(0,100);
$lastproducts = unserialize($_COOKIE['lastproducts']);
$count = 0;
foreach(array_reverse($lastproducts) as $key => $lastproduct) {
if ($count <= 10) {
echo "item[$key]: $lastproduct <BR>";
}
else {
echo "Too many elements. Unsetting item[$key] with value $lastproduct <BR>";
unset($lastproducts[$key]);
}
$count = $count + 1;
}
array_push($lastproducts, $currentitem);
setcookie('lastproducts', serialize($lastproducts), time()+3600);
I'd use array_slice ( http://php.net/array_slice ) perhaps like:
$lastproducts = unserialize($_COOKIE['lastproducts']);
// add on the end ...
$lastproducts[] = $newproduct;
// start at -10 from the end, give me 10 at most
$lastproducts = array_slice($lastproducts, -10);
// ....
You can use array_splice($input, $offset) function for this purpose.
$last_items_count = 10;
if(count($lastproducts) >= $last_items_count) {
$lastproducts = array_splice($lastproducts, count($lastproducts) - $last_items_count);
}
var_dump($lastproducts);
I hope this code helps.
For more information, here is the documentation:
http://php.net/manual/en/function.array-splice.php
I think a better way to select last 10 is:
$selection = array();
foreach(array_reverse($lastproducts) as $key => $lastproduct) {
$selection[$key] = $lastproduct;
if (count($selection)>=10) break;
}
Finally, $selection will have last 10 (or less) products.
Works great using array_splice and array_slice, thanks! :)
$lastproducts = unserialize($_COOKIE['lastproducts']);
// remove this product from array
$lastproducts = array_diff($lastproducts, array($productid));
// insert product on first position in array
array_splice($lastproducts, 0, 0, $productid);
// keep only first 15 products of array
$lastproducts = array_slice($lastproducts, 0, 15);
I have a array list and I want to select two words from this list without repeat they. But I tried many stuff and nothing worked. There is the current code:
$randomq2[] = "one";
$randomq2[] = "two";
$randomq2[] = "three";
srand ((double) microtime() * 1000000);
$getrando1 = rand(0,count($randomq2)-1);
$getrando2 = rand(0,count($randomq2)-1);
$wordone = $getrando1[$conco1];
$wordtwo = $getrando2[$conco2];
This should work for you:
(Here I first get all unique elements from $randomq2 with array_unique(). Then I simply shuffle() the array and at the end I just extract 2 elements from the start with array_slice())
<?php
$randomq2 = array_unique($randomq2);
shuffle($randomq2);
$random = array_slice($randomq2, 0, 2);
print_r($random);
?>
you can go following way:
$getrando1 = rand(0,count($randomq2)-1);
$getrando2 = rand(0,count($randomq2)-1);
while ($getrando2 == $getrando1)
$getrando2 = rand(0,count($randomq2)-1);
also - I'd suggest you to use mt_rand instead of simple rand
I didn't understand your question very well
but try this
<?php
$a=array("a"=>"red","b"=>"green","c"=>"red");
print_r(array_unique($a));
?>
this will remove duplicated values in the array
I have an array of about 100 different random number like this:
$numbers=array(10,9,5,12, ..... .... ... ...);
now i want to make an array of random numbers from this array so that addition of selected numbers will be my given number. example: i may ask to get array of numbers such that, if i add all numbers it will be 100.
i am trying to do it in this way,
function rendom_num ($array,$addition)
{
//here is the code
}
print_r (rendome_num ($numbers,100));
i am not able to fiend the code for last 3 days!
Please use shuffle-
<?php
$numbers = range(1, 20);
shuffle($numbers);
foreach ($numbers as $number) {
echo "$number ";
}
?>
php.net
can use shuffle as #chatfun said or can try array_rand if want only some random values from your array
$value= array("Rabin","Reid","Cris","KVJ","John");
$rand_keys=array_rand($value,2);
echo "First random element = ".$value[$rand_keys[0]];
echo "<br>Second random element = ".$value[$rand_keys[1]];
Something like this should work. The breakdown is commented so you know what it's all doing.
function Randomizer($number = 100)
{
// This just generates a 100 number array from 1 to 100
for($i=1; $i <= 100; $i++) {
$array[] = $i;
}
// Shuffles the above array (you may already have this array made so you would need to input into this function)
shuffle($array);
// Assign 0 as base sum
$sum = 0;
// Go through the array and add up values
foreach($array as $value) {
// If the sum is not the input value and is also less, continue
if($sum !== $number && $sum < $number) {
// Check that the sum and value are not greater than the input
if(($sum + $value) <= $number) {
// If not, then add
$sum += $value;
$new[] = $value;
}
}
// Return the array when value hit
else
return $new;
}
// If the loop goes through to the end without a successful addition
// Try it all again until it does.
if($sum !== $number)
return Randomizer($number);
}
// Initialize function
$test = Randomizer(100);
echo '<pre>';
// Total (for testing)
echo array_sum($test);
// Array of random values
print_r($test);
echo '</pre>';