Jump to content

Recommended Posts

How can i get different random numbers ? For example i need 5 numbers it will pick from 0 to 5 and the 5 numbers won't equal with others and i want to be with the FOR loop.

 

Exapmle i want something like this ;

 

for($ix=0;$ix < 5;$ix++) {

 

$rand_number = rand(0, 5);

                echo $rand_number;

 

}

 

 

I want the numbers to be different from others.

Link to comment
https://forums.phpfreaks.com/topic/243983-getting-different-random-numbers/
Share on other sites

You would need to use RANGE in this case... similiar to Python Lists

 

<?php
$min =0;
$max = 5;
$rand = range($min, $max);
$number = array();
shuffle($rand);

for($i=0;$i<=count($rand);$i++){
  
   $number = $rand;

}
print_r($number);
?>

 

Array
(
    [0] => 0
    [1] => 3
    [2] => 4
    [3] => 2
    [4] => 1
    [5] => 5
)

You would need to use RANGE in this case... similiar to Python Lists

 

<?php
$min =0;
$max = 5;
$rand = range($min, $max);
$number = array();
shuffle($rand);

for($i=0;$i<=count($rand);$i++){
  
   $number = $rand;

}
print_r($number);
?>

 

Array
(
    [0] => 0
    [1] => 3
    [2] => 4
    [3] => 2
    [4] => 1
    [5] => 5
)

 

Thanks for your support. I didn't try this code but i did what i want with your previous code maybe this will work too but i didn't tried :) Thank again :)

Np, hope your still not using the first code, because it WON'T generate the amount of numbers you want specifically. It will populate it with 5 numbers, however if 2 of those 5 numbers are the same it will take one of them away. What you want is a range of 5 total digits with different numbers..

 

so Again, use the second code if you havn't yet, and its my pleasure to help.

 

Mark the Topic As Solved :)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.