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