Php rand and repeat numbers - php

I create this php script for generate random numbers but my problem , these numbers repit in many cases and no get different number whitout repit
> <?php
>
> $products=array("1","2","3","4","5","6","7","8","9","10");
>
> for ($i=0;$i<count($products);$i++) {
>
> $numbers_[$i]=rand(0,count($products));
>
> if ($numbers_[$i]=="") { $numbers_[$i]="1"; } else { if
> ($numbers_[$i]>count($products)) { $numbers_[$i]=="10"; } else {
>
> if ($numbers_[$i]==$numbers_[$i]) { $numbers_[$i]=="*"; }
>
>
> }
>
>
> }
>
>
> print "".$products[$numbers_[$i]]."<br>";
>
> } ?>
Always i need get 5 different numbers .....
This my one problem
Thank´s Regards

If you are after different numbers, just keep generating random numbers until you get 5 different ones.
For each random number you generate, check your array to see if you already have it. If so, don't add it to the array again and keep generating.
The PHP in_array() function makes it easy to check if a value is in your array already.

You query X random numbers with X possible values. They ought to collide.
The code is hard to understand, but I think the condition if ($numbers_[$i]==$numbers_[$i]) is always true. You say you need "5" different numbers but that "5" is nowhere in the code, so I might still be missing a piece...
If you're trying to randomize the order of elements in $products, try shuffle()
To get the numbers between N and M (N<=M) randomized use $data=array(); for($i=N;$i<=M;++$i) $data[]=$i; $result=shuffle($data);

You need "array_rand".
$products = array("1","2","3","4","5","6","7","8","9","10");
$rand_keys = array_rand($products, 5); // Get 5 Only
You can then loop through the returned Keys and print out the Product numbers or any other details.
foreach ($rand_keys as $key) {
echo $products[$key];
}

Related

How to get a specific number of random elements from an array in php?

So, I,been trying to write a code for a "Dice generator" and I want it to generate random numbers (from 1-6) a x numbers of times.
The x number of times is given as a parameter by the user through the terminal, with this "$argc argv" function, but this is not really important.
What I want to know is: how do I generate random values x number of times?
FOR EXAMPLE:
User input: 4
Output: 5 3 6 8
User input: 3
Output: 5 1 2
This is what I am trying to write. I used the array_rand function with the array as a parameter, and the number of times I want as the second parameter, but It does not work! What am I not getting here?
<?php
if ($argc < 2) {
print "You have to write at least one parameter" .PHP_EOL;
exit(1);
}
//This is the variable that corresponds to the number of times the user will give on the Terminal as a parameter.
//$randoms = $argv[1];
$num = 3;
$diceNumbers = [1, 2, 3, 4, 5, 6];
$keys = array_rand($diceNumbers, $num);
print $diceNumbers[$keys[0]]." ".$diceNumbers[$keys[1]] .PHP_EOL;
?>
Given your use case is for a 'dice roll' I wonder if you would be better off using a cryptographically secure random number generation function like random_int() rather than array_rand(), which is not.
You can then use a for loop for your output as you know in advance how many times you want the loop to run.
$num = 3;
for($i = 0; $i < $num; $i++){
print random_int(1,6) . PHP_EOL;
}
The array_rand(arry, n) function returns an array with length n, composed of elements from arry. It looks like you did everything right, however when you print it you only ask for the first 2 random numbers. If you want to print all of the numbers, you will need a for/foreach loop.
foreach($keys as $key) {
print $key . " ";
}

How to generate numbers list starting from 3

I have this PHP code
What this code do is matching month count to numbers .
If condition is true echo result!
Result is displayed in table for each user.
$isT is count of months between 2 dates example 1,2,3 or 9
if($isT=='3'
OR $isT=='6'
OR $isT=='9'
OR $isT=='12'
OR $isT=='15'
OR $isT=='18'
OR $isT=='21'){
//echo something
}
What i want is make this numbers automatically generated
So it would be like:
if($isT==$generatednumber[$i]){
//echo something
}
i need numbers in this order 3 6 9 12 15. ..
basically +3 to the last number
First make sure its more than 0 then check if its multiple of 3.
if($isT >0 && ($isT % 3) == 0)){
//echo something
}
As i said modulus (%) is needed here:-
if(!empty($isT) && ($isT % 3) == 0)){
//echo something
}
Note:- this code will check:-
1.Your variable is set
2.Have some value
3.Is a multiple of 3.

Arrangement : how many unique possibilities for x elements among n

I'm not that good at math, so I'm stuck here.
I need to get the total number of possible arrangement (I think, or permutations maybe?) of X elements amongst N.
I want to pick X distinct elements amongst N (N>=X)
order DOES matter
each element can not come more than once in a combination
=> For exemple, given $N = count(1,2,3,4,5,6,7,8,9), a valid combination of $X=6 elements could be :
- 1,4,5,3,2,8
- 4,2,1,9,7,3
What formula do I need to use in PHP to get the total number of possibilities?
There are N choices for the first element, N-1 for the second (as you have already chosen 1) then N-2 choices for the third and so on. You can express using factorials this a N! / (N-X-1)!. See https://en.wikipedia.org/wiki/Permutations
Ok, I think I got it.
$set = array(A,B,C,D,E,F,G);
$n = count($set);
$k = 6;
if($n>0)
{
if($k < $n)
{
$outcomes = gmp_fact($n) / gmp_fact($n-$k);
}
else
{
$outcomes = gmp_fact($n);
}
} else { $outcomes = 0; }
where gmp_fact($n) is the php function for $n! (n factorial), which means N x (N-1) x ... x 1

PHP if statements for numbers/digits greater than/less than

I have a little puzzle I'm trying to solve with no joy yet. I want to have a set of ifelse statements filter a number variable. Sort of like this.
If the assets are greater than 1500000, make the value down to a 6 digit number maximum using the numbers existing.
If the assets is smaller than 599999, make the value up to a 6 digit maximum using the numbers existing.
If the assets is between 599999 and 1500000, leave the variable alone and let it pass.
if ($assets > 1500000) {
$assets_calc = preg_match_all('/(\d{6})/', $assets_array, $matches);
}
elseif ($assets < 599999) {
$assets_calc = preg_match_all('/someregex here/', $assets_array, $matches);
} else {
$assets = $assets
}
Not sure if that is possible.
Not sure if this answers your question but are you looking to simply truncate digits of each value in the array?
if ($assets > 1500000)
{
foreach($assets_array as $k=>$v)
{
$assets_calc[$k] = (int)substr((string)$v, 0, 6);
}
}
// implement substring() in rest of code here

Php generate random and different numbers

I create this in php for generate random numbers
> <?php
>
> $products=array("1","2","3","4","5","6","7","8","9","10");
>
> for ($i=0;$i<count($products);$i++) {
>
> $numbers=rand(0,count(products));
>
> print "".$products[$numbers]."<br>";
>
> } ?>
I try generate in bucle different numbers but always show me the same numbers 1212121212 and nothing more , how i can generate this string or array and for example finally show 2 3 4 1 5 6 7 9 8 10 , and if reload script other combination
Thank´s Regards !!!
You forgot the $ in count($products). As a result, the parser is treating it as the string "products", which has a count() of 1. Therefore, the rand() function returns zero or one, which in your original array correspond to "1" and "2".
Try using shuffle,
$products=array("1","2","3","4","5","6","7","8","9","10");
shuffle($products);
foreach($products as $v)
echo $v;
Demo

Categories