viion Posted March 27, 2009 Share Posted March 27, 2009 How do I make mt_rand always be a positive number? Link to comment https://forums.phpfreaks.com/topic/151341-make-mt_random-always-positive/ Share on other sites More sharing options...
ratcateme Posted March 27, 2009 Share Posted March 27, 2009 shouldn't it always be positive? the manual says "returns a pseudo-random value between 0 and mt_getrandmax()" and i doubt that mt_getrandmax() would return a negative value. otherwise you could specify a range that would produce positive numbers. Scott. Link to comment https://forums.phpfreaks.com/topic/151341-make-mt_random-always-positive/#findComment-794895 Share on other sites More sharing options...
viion Posted March 27, 2009 Author Share Posted March 27, 2009 Well it seems to be working now, but how do i make it like, I want a specific number of random numbers. Like a 12 digit number, rather than some random numbers being 5 digit long, or some 10 digit long. Link to comment https://forums.phpfreaks.com/topic/151341-make-mt_random-always-positive/#findComment-794901 Share on other sites More sharing options...
ratcateme Posted March 27, 2009 Share Posted March 27, 2009 like i said specify a range like mt_rand (1*pow(10*12),1*pow(10*13)-1); untested but i think it should work Scott. Link to comment https://forums.phpfreaks.com/topic/151341-make-mt_random-always-positive/#findComment-794904 Share on other sites More sharing options...
dadamssg Posted March 27, 2009 Share Posted March 27, 2009 mt_rand() uses two parameters..the first being the lowest value it can randomly generate and the second being the highest...so if you always want a positive 12 digit number you would do mt_rand(100000000000,999999999999); Link to comment https://forums.phpfreaks.com/topic/151341-make-mt_random-always-positive/#findComment-794940 Share on other sites More sharing options...
Mchl Posted March 27, 2009 Share Posted March 27, 2009 Are you all aware, that these numbers are above 32bit integer range? Link to comment https://forums.phpfreaks.com/topic/151341-make-mt_random-always-positive/#findComment-794943 Share on other sites More sharing options...
dadamssg Posted March 27, 2009 Share Posted March 27, 2009 yeah i just tried what i said and it definitely didn't work...so heres something i put together that does...maybe not the most efficient but it works nonetheless... $number = abs(mt_rand(100,999)); $ab = abs(mt_rand(100,999)); $bc = abs(mt_rand(100,999)); $cd = abs(mt_rand(100,999)); echo $number.$ab.$bc.$cd; Link to comment https://forums.phpfreaks.com/topic/151341-make-mt_random-always-positive/#findComment-794945 Share on other sites More sharing options...
ratcateme Posted March 27, 2009 Share Posted March 27, 2009 Are you all aware, that these numbers are above 32bit integer range? im not very up with the play on how PHP handles large numbers but php.net/int suggest numbers outside the 32bit range would be changed to a float that has a 64bit range that supports up to around 14 decimal digits. Scott. Link to comment https://forums.phpfreaks.com/topic/151341-make-mt_random-always-positive/#findComment-794947 Share on other sites More sharing options...
dadamssg Posted March 27, 2009 Share Posted March 27, 2009 yeah...i just thought about that. so then you would just do something like $number = abs(mt_rand(100,999)); $ab = abs(mt_rand(100,999)); $bc = abs(mt_rand(100,999)); $cd = abs(mt_rand(100,999)); $randomstring = $number.$ab.$bc.$cd; $randomnumber = int($random); then you have a random positive 12-digit integer....done and done haha Link to comment https://forums.phpfreaks.com/topic/151341-make-mt_random-always-positive/#findComment-794951 Share on other sites More sharing options...
dadamssg Posted March 27, 2009 Share Posted March 27, 2009 oops meant this haha $number = abs(mt_rand(100,999)); $ab = abs(mt_rand(100,999)); $bc = abs(mt_rand(100,999)); $cd = abs(mt_rand(100,999)); $random_string = $number.$ab.$bc.$cd; $random_number = intval($number.$ab.$bc.$cd); echo $random_string; Link to comment https://forums.phpfreaks.com/topic/151341-make-mt_random-always-positive/#findComment-794952 Share on other sites More sharing options...
ratcateme Posted March 27, 2009 Share Posted March 27, 2009 with php you don't even really need to cast it as a int if you just went $random_string + 1 it should work just the same as a normal number defined as $number = 55 Scott. Link to comment https://forums.phpfreaks.com/topic/151341-make-mt_random-always-positive/#findComment-794955 Share on other sites More sharing options...
Yesideez Posted March 27, 2009 Share Posted March 27, 2009 I've never had a problem with mt_rand(). If you want a random number between 1 and 6 (rolling a di) $di=mt_rand(1,6); Between 1 and 24 (random hour of the day) $di=mt_rand(1,24); There's no need to abs() the result as it always returns a positive integer if you only supply positive inputs. Link to comment https://forums.phpfreaks.com/topic/151341-make-mt_random-always-positive/#findComment-794959 Share on other sites More sharing options...
dadamssg Posted March 27, 2009 Share Posted March 27, 2009 yes but he wants a 12-digit number...a number higher than 32-bit apparently...so you would need to shove a bunch together then intval() it to make it a number(64-bit) again...if he wants to do math calcs with it...otherwise, he could just leave it as a string..and you're right about the abs value thing, it is unnecessary Link to comment https://forums.phpfreaks.com/topic/151341-make-mt_random-always-positive/#findComment-794962 Share on other sites More sharing options...
Mchl Posted March 27, 2009 Share Posted March 27, 2009 Except this code will never generate 100000000000 and similar numbers. How about something like this $number = mt_rand(100000,999999) * 1000000 + mt_rand(0,999999); Link to comment https://forums.phpfreaks.com/topic/151341-make-mt_random-always-positive/#findComment-794964 Share on other sites More sharing options...
Yesideez Posted March 27, 2009 Share Posted March 27, 2009 Ah! Anything above a 32bit value will result in very incorrect values as numbers start to "wrap around" when outside of the limits. There's probably a set of scripts somewhere for handling larger values - I've seen one with dates outside of the normal range so I can't see why there isn't one for integers. Link to comment https://forums.phpfreaks.com/topic/151341-make-mt_random-always-positive/#findComment-794968 Share on other sites More sharing options...
dadamssg Posted March 27, 2009 Share Posted March 27, 2009 i didn't think about that....but technically, though the chances are TINY, if the first mt_rand() is 999999 and the second is 999999 youd get 1,999,997,000,001 thirteen digits Link to comment https://forums.phpfreaks.com/topic/151341-make-mt_random-always-positive/#findComment-794969 Share on other sites More sharing options...
Mchl Posted March 27, 2009 Share Posted March 27, 2009 Of course there is bcmath dadamssg: can't see how 999,999,000,000 + 999,999 gives 1,999,997,000,001 Link to comment https://forums.phpfreaks.com/topic/151341-make-mt_random-always-positive/#findComment-794973 Share on other sites More sharing options...
Yesideez Posted March 27, 2009 Share Posted March 27, 2009 Imagine you have an unsigned tinyint - this ranges from 0 to 255. If you add 1 to it you don't get 256 because a tinyint can't be 256 - its upper limit is 255. I think it goes back to 0 - wraps around. Same goes for higher numbers when outside their limits. Link to comment https://forums.phpfreaks.com/topic/151341-make-mt_random-always-positive/#findComment-794978 Share on other sites More sharing options...
Mchl Posted March 27, 2009 Share Posted March 27, 2009 If PHP encounters a number beyond the bounds of the integer type' date=' it will be interpreted as a float instead. Also, an operation which results in a number beyond the bounds of the integer type will return a float instead. [/quote'] The only problem is, mt_rand will always return 2^31-1 when the range is above 2^31. Link to comment https://forums.phpfreaks.com/topic/151341-make-mt_random-always-positive/#findComment-794979 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.