Mgccl Posted February 5, 2007 Share Posted February 5, 2007 I have to generate a very large random number between $min and $max This number is larger than the largest number PHP can handle, so I have to output it as a string I want to know how to make one. I have seen one of them that works only in *nix. Link to comment https://forums.phpfreaks.com/topic/37159-generate-a-very-large-random-number/ Share on other sites More sharing options...
Balmung-San Posted February 5, 2007 Share Posted February 5, 2007 How many digits are you looking for here? Since PHP can't handle the size of the number I would suggest just doing a for loop to get the number of digits you want, then randomizing each digit, and adding it onto the string. Link to comment https://forums.phpfreaks.com/topic/37159-generate-a-very-large-random-number/#findComment-177471 Share on other sites More sharing options...
ted_chou12 Posted February 5, 2007 Share Posted February 5, 2007 function generate_password($length= { $password = ""; $possible = "0123456789"; $i = 0; while ( $i < $length ) { $char = substr($possible, mt_rand(0, strlen($possible)-1), 1); $password .= $char; $i++; } return $password; } $password = generate_password($length=;//8 can be optimzed to the no. of digits you want. As long as php can run, i guess however many digits you wish, it can be generated, but processing it such as division or multiplication, im not sure. btw. basically used the random pass. just limited the string to numeric. Ted Link to comment https://forums.phpfreaks.com/topic/37159-generate-a-very-large-random-number/#findComment-177583 Share on other sites More sharing options...
Mgccl Posted February 6, 2007 Author Share Posted February 6, 2007 Thx guys. Here is the script I have made, kinda fast function bcrand($min, $max){//input STRING! $top = bcsub($max,$min); $rand = bcadd($top, 1); $length = strlen($top); while(bccomp($rand,$top)==1){ unset($rand_part); $n = 0; while(9*$n <= $length){ if($length - 9*$n >= 9){ $rand_part[] = rand(0,999999999); }else{ $j = 0; $foo = ''; while($j < $length-9*$n){ $foo .= '9'; ++$j; } $foo += 0; $rand_part[] = rand(0,$foo); } ++$n; } $i = 0; $rand =''; $count = count($rand_part); while($i < $count){ $rand .= $rand_part[$i]; ++$i; } } return bcadd($rand,$min); } Link to comment https://forums.phpfreaks.com/topic/37159-generate-a-very-large-random-number/#findComment-177811 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.