1. ## Generate a random number that doesn't already exist

i want to generate a unique random number but cant work out how to do it??

this is how far i got but i know it wont work, can someone help??

PHP Code:
``` \$already_exists = array(1,3,4,6,7,8); \$pot = rand(0,10000); if(in_array(\$pot, \$already_exists)){    \$pot = rand(0,1000); }  ```

2. You could probably use a do-while loop and create your number inside. You would put your in_array() statement as the condition.

http://us3.php.net/manual/en/control...s.do.while.php

3. PHP Code:
``` \$already_exists = array(1,3,4,6,7,8);do \$pot = rand(0,10);while(in_array(\$pot, \$already_exists));var_dump(\$pot);  ```

4. excellent thanks for your help!

5. Originally Posted by JackRabbitSlims
i want to generate a unique random number but cant work out how to do it??
this is how far i got but i know it wont work, can someone help??
PHP Code:
``` \$already_exists = array(1,3,4,6,7,8); \$pot = rand(0,10000); if(in_array(\$pot, \$already_exists)){    \$pot = rand(0,1000); }  ```
I think you are not clear for what purpose you want those unique numbers and how many numbers you want. If you mean you want 100 numbers from 0 to 1000, with help of any iteration (loop) you can achieve easily:
Code php:
```\$arr 		= array();
\$howmany 	= 100; // you want to achieve 100 unique numbers
while(1){
\$num = rand(0, 10000);
if(!in_array(\$num, \$arr))
\$arr[] = \$num;
if(count(\$arr) == 100)
break;
}
echo "<pre>";
print_r(\$arr);
echo "</pre>";```
l

6. sorry for not being clear, i only wanted one number, that was between 2 numbers, that wasn't already in an array of numbers,

thanks for the response to my query

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•