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 Quote 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. Quote 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? Quote 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 Quote 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]) Quote 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 Quote Link to comment https://forums.phpfreaks.com/topic/146272-solved-rand-help/#findComment-767935 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.