SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2006
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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. #2
    SitePoint Addict
    Join Date
    Oct 2003
    Location
    United States
    Posts
    281
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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. #3
    SitePoint Addict Wildhoney's Avatar
    Join Date
    Apr 2006
    Location
    Nottingham
    Posts
    246
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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); 
    TalkPHP.com - The Friendly PHP Community

    Watch Reaper Online - Watch Chuck Online

  4. #4
    SitePoint Enthusiast
    Join Date
    Jul 2006
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    excellent thanks for your help!

  5. #5
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by JackRabbitSlims View Post
    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
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  6. #6
    SitePoint Enthusiast
    Join Date
    Jul 2006
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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


Bookmarks

Posting Permissions

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