silversand Posted March 29, 2011 Share Posted March 29, 2011 I'm hoping someone can show me how to get a random decimal number. I've googled and can't find anything that works. I need to find a random number between 1.0 and 10.0 I can get a random whole number no problem, but decimals aren't working. When I finally get that working, ideally I'd like to be able to find a random number between two decimals (like above) but also have a small chance of getting a decimal slightly higher than the two given. So, for example, maybe a random decimal between 3.5 and 6.0, but a slight chance of getting 6.2? Thank you very much for any help! Quote Link to comment https://forums.phpfreaks.com/topic/232069-random-decimal-number/ Share on other sites More sharing options...
mikecampbell Posted March 29, 2011 Share Posted March 29, 2011 It depends how many decimal places you want, but you could use mt_rand(0, 100)/10; to get numbers between 0 and 10 with one decimal place. Quote Link to comment https://forums.phpfreaks.com/topic/232069-random-decimal-number/#findComment-1193717 Share on other sites More sharing options...
silversand Posted March 29, 2011 Author Share Posted March 29, 2011 I've tried that and I get a whole number or a 0. This is the solution I found while googling but for some reason it never works for me. Quote Link to comment https://forums.phpfreaks.com/topic/232069-random-decimal-number/#findComment-1193723 Share on other sites More sharing options...
mikecampbell Posted March 29, 2011 Share Posted March 29, 2011 It works for me. Are you echoing the random number immediately after you generate it? Quote Link to comment https://forums.phpfreaks.com/topic/232069-random-decimal-number/#findComment-1193733 Share on other sites More sharing options...
kenrbnsn Posted March 29, 2011 Share Posted March 29, 2011 Please post your code between tags. Ken Quote Link to comment https://forums.phpfreaks.com/topic/232069-random-decimal-number/#findComment-1193736 Share on other sites More sharing options...
silversand Posted March 29, 2011 Author Share Posted March 29, 2011 No, the number is generated and inserted into the database first and then displayed on the page when called. Even when I check the database the number is still whole or 0. Quote Link to comment https://forums.phpfreaks.com/topic/232069-random-decimal-number/#findComment-1193738 Share on other sites More sharing options...
kenrbnsn Posted March 29, 2011 Share Posted March 29, 2011 How is the database field defined? Ken Quote Link to comment https://forums.phpfreaks.com/topic/232069-random-decimal-number/#findComment-1193742 Share on other sites More sharing options...
silversand Posted March 29, 2011 Author Share Posted March 29, 2011 $headgenes = mt_rand(0, 100)/10; mysql_query("INSERT INTO ".$prefix."owned VALUES ('', '$d[$i]', '$name[$i]','$loggedinname','0','0','0','0','0','0','0', '$code', '','$alts','ftrade','no','$gend','$headgenes')"); Quote Link to comment https://forums.phpfreaks.com/topic/232069-random-decimal-number/#findComment-1193744 Share on other sites More sharing options...
cyberRobot Posted March 29, 2011 Share Posted March 29, 2011 Is the field type set to something that can hold decimals? If you're using MySQL, you might want to review: http://dev.mysql.com/doc/refman/5.0/en/numeric-types.html Quote Link to comment https://forums.phpfreaks.com/topic/232069-random-decimal-number/#findComment-1193745 Share on other sites More sharing options...
silversand Posted March 29, 2011 Author Share Posted March 29, 2011 Aaaaah, I think they may be it. The mysql field is set to int. I'm off to go fix that now and will report back. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/232069-random-decimal-number/#findComment-1193747 Share on other sites More sharing options...
silversand Posted March 29, 2011 Author Share Posted March 29, 2011 Hmm, I changed the mysql to decimal but I still end up with a whole number being inserted... Quote Link to comment https://forums.phpfreaks.com/topic/232069-random-decimal-number/#findComment-1193751 Share on other sites More sharing options...
cyberRobot Posted March 29, 2011 Share Posted March 29, 2011 Does it display the decimal part if you print $headgenes before the SQL query? For example: <?php ... $headgenes = mt_rand(0, 100)/10; echo $headgenes; mysql_query("INSERT INTO ".$prefix."owned VALUES... ?> Quote Link to comment https://forums.phpfreaks.com/topic/232069-random-decimal-number/#findComment-1193756 Share on other sites More sharing options...
silversand Posted March 29, 2011 Author Share Posted March 29, 2011 Yes. It just doesn't seem to want to store as a decimal in the database Quote Link to comment https://forums.phpfreaks.com/topic/232069-random-decimal-number/#findComment-1193759 Share on other sites More sharing options...
cyberRobot Posted March 29, 2011 Share Posted March 29, 2011 What happens if you display the query; is the decimal still there? Note that you may need to modify the code a little: <?php ... $sql = "INSERT INTO ".$prefix."owned VALUES ('', '$d[$i]', '$name[$i]','$loggedinname','0','0','0','0','0','0','0', '$code', '','$alts','ftrade','no','$gend','$headgenes')"; echo $sql; mysql_query($sql); ... ?> Quote Link to comment https://forums.phpfreaks.com/topic/232069-random-decimal-number/#findComment-1193763 Share on other sites More sharing options...
silversand Posted March 29, 2011 Author Share Posted March 29, 2011 No, no decimal. Quote Link to comment https://forums.phpfreaks.com/topic/232069-random-decimal-number/#findComment-1193767 Share on other sites More sharing options...
cyberRobot Posted March 29, 2011 Share Posted March 29, 2011 So what does the code between "$headgenes = mt_rand(0, 100)/10;" and the SQL query look like? Quote Link to comment https://forums.phpfreaks.com/topic/232069-random-decimal-number/#findComment-1193770 Share on other sites More sharing options...
silversand Posted March 29, 2011 Author Share Posted March 29, 2011 Wait! Just did it again and there IS a decimal there when I echo it. This is what came up. INSERT INTO owned VALUES ('', '', '','','0','0','0','0','0','0','0', '2131', '','','ftrade','no','','6.5' ) Still doesn't work when I try to use it in the code and display it from the database. Quote Link to comment https://forums.phpfreaks.com/topic/232069-random-decimal-number/#findComment-1193772 Share on other sites More sharing options...
cyberRobot Posted March 29, 2011 Share Posted March 29, 2011 It sounds like you changed the database field to "decimal"; what did you enter for the "Length/Values" part of the field? Note that it needs to be set to something like "6,2" where the number after the comma represents how many decimal places you want to go. Quote Link to comment https://forums.phpfreaks.com/topic/232069-random-decimal-number/#findComment-1193775 Share on other sites More sharing options...
silversand Posted March 29, 2011 Author Share Posted March 29, 2011 Woohooo! That was it! I didn't have the length/values set correctly. Thank you! Thank you! Can I press my luck for my other question? How to get a random decimal between two numbers with a chance of a slightly higher result? So the result from a number between 2.2 and 7.1 would be something like 5.4, but could possibly be 7.2? Quote Link to comment https://forums.phpfreaks.com/topic/232069-random-decimal-number/#findComment-1193779 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.