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?

Recommended Answers

All 13 Replies

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
?>

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.

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
?>

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

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

Thanks a lot!! :)

your welcome.
if your problem is solved then mark this forum as solved.

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..

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

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. :-)

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.

if there is a bug on the code we cannot marked it as solved :-)

mine returns 5 everytime

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.