smithmr8 Posted April 19, 2008 Share Posted April 19, 2008 Hi, I'd like to know how to generate random numbers between the values 0.100 and 1.000 The number must be 3 decimal places. Thanks Link to comment https://forums.phpfreaks.com/topic/101854-random-number/ Share on other sites More sharing options...
woobarb Posted April 19, 2008 Share Posted April 19, 2008 Probably an easier way but here's one: $n = rand(100, 1000); $n /= 1000; $n = sprintf("%0.3f", $n); print $n; Link to comment https://forums.phpfreaks.com/topic/101854-random-number/#findComment-521267 Share on other sites More sharing options...
smithmr8 Posted April 19, 2008 Author Share Posted April 19, 2008 Works like a charm. Thanks alot. Link to comment https://forums.phpfreaks.com/topic/101854-random-number/#findComment-521268 Share on other sites More sharing options...
logicopinion Posted April 19, 2008 Share Posted April 19, 2008 <?php function rand_img($lenght=5) { $number = ""; for ($i = 1; $i <= $lenght; $i++) { $number .= rand(0,9).""; } $width = 11*$lenght; $height = 30; $img = ImageCreate($width, $height); $background = imagecolorallocate($img,255,255,255); $color_black = imagecolorallocate($img,0,0,0); $color_grey = imagecolorallocate($img,169,169,169); imagerectangle($img,0, 0,$width-1,$height-1,$color_grey); imagestring($img, 5, $lenght, 7, $number, $color_black); header('Content-Type: image/png'); imagepng($img); imagedestroy($img); } rand_img(); ?> Link to comment https://forums.phpfreaks.com/topic/101854-random-number/#findComment-521269 Share on other sites More sharing options...
Fadion Posted April 19, 2008 Share Posted April 19, 2008 woobarb solution works correctly, but anyway here's a solution i found on php.net and added only the sprintf(): <?php function random_float($min,$max){ return sprintf("%0.3f", ($min+lcg_value()*(abs($max-$min)))); } echo random_float(0.01, 1); ?> Link to comment https://forums.phpfreaks.com/topic/101854-random-number/#findComment-521274 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.