almightyegg Posted February 21, 2009 Share Posted February 21, 2009 is there any way to get a random value with decimals?? eg. rand(45.21, 56.89) = 51.34 Link to comment https://forums.phpfreaks.com/topic/146272-solved-rand-help/ Share on other sites More sharing options...
Philip Posted February 21, 2009 Share Posted February 21, 2009 As taken from a php.net comment: <?php function random_float ($min,$max) { return ($min+lcg_value()*(abs($max-$min))); } echo random_float(1,7); ?> You could then take it to whatever decimal you wanted. Link to comment https://forums.phpfreaks.com/topic/146272-solved-rand-help/#findComment-767928 Share on other sites More sharing options...
almightyegg Posted February 21, 2009 Author Share Posted February 21, 2009 I know have 108.411944095 hehe, how would I change it to 2 dp? Link to comment https://forums.phpfreaks.com/topic/146272-solved-rand-help/#findComment-767931 Share on other sites More sharing options...
Philip Posted February 21, 2009 Share Posted February 21, 2009 <?php function random_float ($min,$max) { return ($min+lcg_value()*(abs($max-$min))); } echo round(random_float(1,7),2); ?> There is probably a better way, but that's just one way Link to comment https://forums.phpfreaks.com/topic/146272-solved-rand-help/#findComment-767933 Share on other sites More sharing options...
Cal Posted February 21, 2009 Share Posted February 21, 2009 Use round(); round([number], [decimalplaces]) Link to comment https://forums.phpfreaks.com/topic/146272-solved-rand-help/#findComment-767934 Share on other sites More sharing options...
almightyegg Posted February 21, 2009 Author Share Posted February 21, 2009 oh yeah! duh thanks Link to comment https://forums.phpfreaks.com/topic/146272-solved-rand-help/#findComment-767935 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.