Php generate random and different numbers - php

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

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 . " ";
}

I want to print numbers pattern program but it not give me wrong out put

I want to get my output like this
1 5 9 13
2 6 10 14
3 7 11 15
4 8 12 16
I am trying like this:
<?php
for($a=1; $a<=16; $a++)
{
for($b=$a; $b>=1; $b--)
{
echo "$b";
}
echo "<br>";
}
?>
The above code gives me the wrong output.
Let's debug.
You are starting from 1 in your outer loop and in your inner loop, you are going from $a till 1 times.
This doesn't comply with your requirements because we have to print an increasing sequence in each row.
You can also notice that every number in a row differs by 4.
So, logic would be like below:
Pseudocode:
rows = 4
starting_number = 1
loop from 1 to rows
number = starting_number
loop from 1 to 4 // since each row has 4 numbers
print number
number += 4
print new_line
starting_number++
Demo: https://3v4l.org/9YjIP

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.

generating random integer with 10 numbers always start with 1

i am using this code
<?php
function random()
{
return rand(1111111111,9999999999);
};
for ($x = "1";$x <= "5";$x++)
{
echo $x." : ".random()."<br>";
};
echo "<hr>";
?>
some outputs :
1 : 1303960718
2 : 1308203081
3 : 1280148745
4 : 1263151923
5 : 1124814399
i tried generating more numbers and all of it starts with 1
i tried to used rand() directly and the same thing happend
Run this code and you will get your answer yourself
return rand(2147483647,9999999999);
Then try running
echo getrandmax();
Depending upon your system you might get something like 2147483647
That means your upper limit is pretty much useless beyond that number. And on certain systems that max can even be lower than that. You also have to research about integer overflow.
Now if you were to go easy on your system and remove 1 digit from your number and make the new range
return rand(111111111,999999999);
Then your code would work just fine, because there are no overflows.

Php rand and repeat numbers

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];
}

Categories