xyn Posted August 10, 2006 Share Posted August 10, 2006 Heyy Guys,I have my little random number string, I can't seem to work-outhow to make it generate a random 8 number code. sometimes itis 1 number or others it is 2 numbers :/.[code=php:0]$numbers = array('1','2','3','4','5','6','7','8','9'); for ($i=8; $i<10; $i++) { $Keyarray[$i] = $numbers[rand(0,count($numbers))]; } shuffle($Keyarray); $Newkey = implode("",$Keyarray); echo "EUM$Newkey"; exit;[/code] Quote Link to comment https://forums.phpfreaks.com/topic/17124-random-8-numbers-string/ Share on other sites More sharing options...
tomfmason Posted August 10, 2006 Share Posted August 10, 2006 here is a function that you can use. It will return a random number that is eight digits long.[code=php:0]function random_number() { $salt = "0123456789"; srand((double)microtime()*1000000); $i = 0; while ($i <= 8) { $num = rand() % 33; $tmp = substr($salt, $num, 1); $num = $num . $tmp; $i++; } return $num; } [/code]Now you can use this function like this.[code=php:0]$random_number = random_number();echo "$random_number";[/code] Quote Link to comment https://forums.phpfreaks.com/topic/17124-random-8-numbers-string/#findComment-72430 Share on other sites More sharing options...
xyn Posted August 10, 2006 Author Share Posted August 10, 2006 that only outputs double figures :/ I need 8. Quote Link to comment https://forums.phpfreaks.com/topic/17124-random-8-numbers-string/#findComment-72445 Share on other sites More sharing options...
sasa Posted August 10, 2006 Share Posted August 10, 2006 $lenght_of_key = 8;$key='EUM';for ($i=0; $i<$lenght_of_key; $i++) $key .=rand(1,9);echo $key; Quote Link to comment https://forums.phpfreaks.com/topic/17124-random-8-numbers-string/#findComment-72450 Share on other sites More sharing options...
poirot Posted August 10, 2006 Share Posted August 10, 2006 An easier way:[code=php:0]$num = sprintf("%08d", rand(0,99999999));[/code] Quote Link to comment https://forums.phpfreaks.com/topic/17124-random-8-numbers-string/#findComment-72454 Share on other sites More sharing options...
kenrbnsn Posted August 10, 2006 Share Posted August 10, 2006 What's wrong with:[code]<?php$key = rand(10000000,99999999);echo $key;?>[/code]Ken(poirot beat me to it by 9 seconds .. GMTA :) ) Quote Link to comment https://forums.phpfreaks.com/topic/17124-random-8-numbers-string/#findComment-72455 Share on other sites More sharing options...
xyn Posted August 10, 2006 Author Share Posted August 10, 2006 thanks guys :] Quote Link to comment https://forums.phpfreaks.com/topic/17124-random-8-numbers-string/#findComment-72461 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.