0

hello all! :)

Well this is my code:

for ($i=0; $i<5; $i++)
              {   
                    $random= array_rand($ids);         
                    echo "<br> $random generated";         	   	   if ($check[$random]<>0)
                    {
                          $i--;
                          echo " number generated again";
                      } 
                     else
	    {
                        $check[$random]=1;               // set check to 1 to  know the array number has been used
                   	
		    }       
		
			
        } 
	$rnd_num[]= $ids[$random];

	echo "<br> $ids[$random]"; // storing the unique ids in array

I am not being able to store the unique numbers in the array $rnd_num
Can anyone help me plz?

4
Contributors
13
Replies
14
Views
7 Years
Discussion Span
Last Post by kireol
0

is this what you are trying to do?

<?php
$ids = array();
for ($i=0; $i<5; $i++)
{
    $random= rand(0,10);
    echo "<br> $random generated";
    if ($ids[$random]<>0)
    {
        $i--;
        echo " number generated again";
    }
    else
    {
        $ids[$random]=1;               // set check to 1 to  know the array number has been used
    }


}
echo "<br .>".print_r($ids); // storing the unique ids in array
?>
0

I have tried $random = array_rand($ids,10) but im getting the illegal offset type error...Actually, the code below works well.

for ($i=0; $i<5; $i++)
{   $random= array_rand($ids) ;
      echo "<br> $random generated";         	   	
      if ($check[$random]<>0)
     {     $i--;
            echo " number generated again";
    } 
    else
  {
     $check[$random]=1;                              	
}       

        }

when i try it, i get
12 generated
9 generated
12 generated number generated again
9 generated number generated again
14 generated
13 generated
6 generated
What i want to do is store the values 12, 9 14, 13 and 6 in an array.

0

Try this.

<?php
$ids = array();
for ($i=0; $i<5; $i++)
{
    $random= rand(0,10);
	$ids[] = $random;
}

$result = array_unique($ids); //GET THE UNIQUE VALUE ONLY

echo "<br .>".print_r($result); // storing the unique ids in array
?>

Edited by phpbeginners: n/a

0

I am getting this a s output
Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 [7] => 8 [8] => 9 [9] => 10 [10] => 11 [11] => 12 [12] => 13 [13] => 14 [14] => 15 [15] => 16 [16] => 17 [17] => 18 [18] => 19 [19] => 20 [20] => 21 [21] => 22 [22] => 23 [23] => 24 [24] => 25 [25] => [26] => [27] => [28] => [29] => )
1
I forgot to mention that I am actually generating 5 numbers out of 25

0

Use the Below Code for Your Problem:

<?PHP
$ids = array();
for ($i=0; $i<5; $i++)
{
	$size1 = 1;
	$size2 = 1000000;
	$random = rand($size1, $size2);
	$ids[] = $random;
}
echo "<br .>".print_r($ids);
?>

The above Code is Tested and Worked.
Output:

Array ( [0] => 758820 [1] => 197449 [2] => 554902 [3] => 126587 [4] => 862214 ) 
1
0

there is a "Bug" on that hemgoyal_1990.
that one will not return constant 5 numbers;

Array ( [0] => 6627 [1] => 855860 [2] => 739942 [3] => 903183 [4] => 137180 ) 

see Array[0]=>6627


$ids = array();
for ($i=0; $i<5; $i++)
{
	$size1 = 0000000; // <-- change this
	$size2 = 9999999;
	$random = rand($size1, $size2);
	$ids[] = $random;
}
echo "<br .>".print_r($ids);

I thought you are just looking for the elements of your array not to produce any 5 unique numbers :-).

if your problem solved mark it..

Edited by phpbeginners: n/a

0

it was solved with the 1st response. the one that I posted :P You guys just copied what I did and reposted it

Edited by kireol: n/a

0

find the difference kireol there is bug in your code this will not return 5 constant numbers. Try it and you will see. Even the code of "hemgoyal_1990" there is a bug. :-)

0

may be..
when am tested my code this will give me the five number. so i think that this will work nice. so am post my code here.
anyway thanx for fix bug in my code.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.