Random Number generator with Prefix

Discussion in 'PHP' started by medialab, Nov 7, 2011.

  1. #1
    Hi,

    I assume what I am trying to do is easy, I just have no idea what the code would be??

    I need to generate some numbers (50,000 of them to be exact)

    Here are the specifications:

    - Add a Prefix to the numbers - A#####
    - Be 6 digits long (including prefix, so 5 numbers)
    - All be unique
    -Display as a standard list (plain text with return breaks)

    Is there a PHP snippet I could use to generate this list. I only need to use it once, any help would be much appreciated!!!

    EXAMPLE:

    A83754
    A28475
    A90982
    A33382
     
    medialab, Nov 7, 2011 IP
  2. dazst

    dazst Active Member

    Messages:
    115
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    78
    #2
    <?php
    do
    {
            $num = "A" .rand(10000,99999);
            if(@$list[$num]) continue;
            $list[$num] = $num;
            if(count($list) > 50000)
            break;
    } while(1);
    foreach($list as $k => $num)
            echo "$k\n";
    
    
    Code (markup):
     
    Last edited: Nov 7, 2011
    dazst, Nov 7, 2011 IP
  3. uocnv

    uocnv Member

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    31
    #3
    Hopefully this will help.
     
    uocnv, Nov 7, 2011 IP
  4. rusianace

    rusianace Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Hi,
    I've made this snippet for you:
    
    
    <?php
    function series($limit, $prefix='A', $len=5, $print = true) 
    {
    	if($limit <= 0) {
    		echo 'You must set limit!';
    		return;
    	}
    	$lower = floatval('1'.str_repeat('0',$len-1));
    	$upper = floatval('1'.str_repeat('0',$len))-1;
    	$series = array();
    	while(count($series) < $limit) {
    		$num = $prefix.rand($lower, $upper);
    		if(!isset($series[$num])) {
    			$series[$num] = $num;
    		}
    	}
    	$series = array_keys($series);
    	if(!$print) {
    		return $series;
    	} else {
    		foreach($series as $str) {
    			echo $str, "\n";
    		}
    	}
    }
    
    // usage:
    series(50000, 'A', 5);
    // or 
    series(50000);
    
    PHP:
    Hope it works as you needed.
     
    rusianace, Nov 7, 2011 IP
  5. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #5
    Here's what I use for generating random numbers. Just loop though 50,000 times and echo the output.

    
    
    function random_string($length=5, $current_array=array(), $prefix='A')
    {
        $string = '';
        $possible = '0123456789';
    
        for ($i = 1; $i <= $length; $i++) {
            $char = substr($possible, mt_rand(0, strlen($possible) - 1), 1);
            $string .= $char;
        }
    
        if (!in_array($prefix . $string, $current_array)) {
            return $prefix . $string;
        } else {
            return random_string(5, $current_array);
        }
    }
    
    $current_array = array();
    
    for ($i = 1; $i <= 50000; $i++) {
        $current_array[] = random_string(5,$current_array);
    }
    
    echo implode("<br />", $current_array);
    
    
    PHP:
     
    Last edited: Nov 8, 2011
    jestep, Nov 8, 2011 IP
  6. medialab

    medialab Well-Known Member

    Messages:
    366
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    138
    Digital Goods:
    1
    #6
    thanks everyone!!
     
    medialab, Nov 17, 2011 IP