Monkuar Posted September 18, 2012 Share Posted September 18, 2012 <?php echo rand ( 1000 , 9999 ) ?> <br> <?php echo rand(0,9).rand(0,9).rand(0,9).rand(0,9) ?> generating these 2 0000 to 9999 random numbers? doesn't matter which way right? both will always be 0 - 9 correct? which way would be the "proper" way? Quote Link to comment https://forums.phpfreaks.com/topic/268488-whats-the-difference-between/ Share on other sites More sharing options...
Psycho Posted September 18, 2012 Share Posted September 18, 2012 No, they are not the same rand(1000 , 9999) will produce a number between 1,000 and 9,999. While rand(0,9).rand(0,9).rand(0,9).rand(0,9) will produce a number between 0 and 9,999. You could, of course, change the first call in that series to be rand(1, 9) to get the same results. but, that second example is just ridiculous IMO. Quote Link to comment https://forums.phpfreaks.com/topic/268488-whats-the-difference-between/#findComment-1378795 Share on other sites More sharing options...
scootstah Posted September 18, 2012 Share Posted September 18, 2012 I'm interested in the difference in "randomness" between the two. Obviously you get a 4 digit random number either way, but is there a difference? Any math wiz's on? Quote Link to comment https://forums.phpfreaks.com/topic/268488-whats-the-difference-between/#findComment-1378801 Share on other sites More sharing options...
Psycho Posted September 18, 2012 Share Posted September 18, 2012 I'm interested in the difference in "randomness" between the two. Obviously you get a 4 digit random number either way, but is there a difference? Any math wiz's on? Assuming you set them up to have the same total min and the same total max there is no difference in "randomness" Using rand(0, 9999); there is a 1 in 10,000 chance of any possible option. Using rand(0,9).rand(0,9).rand(0,9).rand(0,9) there is a 1 in 10 chance for any specific digit. 10 x 10 x 10 x 10 = 10,000 Quote Link to comment https://forums.phpfreaks.com/topic/268488-whats-the-difference-between/#findComment-1378803 Share on other sites More sharing options...
Monkuar Posted September 18, 2012 Author Share Posted September 18, 2012 No, they are not the same rand(1000 , 9999) will produce a number between 1,000 and 9,999. While rand(0,9).rand(0,9).rand(0,9).rand(0,9) will produce a number between 0 and 9,999. You could, of course, change the first call in that series to be rand(1, 9) to get the same results. but, that second example is just ridiculous IMO. oops, i did mean 0, sorry about that, ok well i just used the 0,9999 instead as it's shorter code... blah thanks all edit, wait nevermind actually 0000,9999 bcz i cant have numbers below 1000 edit, wait nevermind i just will use $db->query('UPDATE daily4 set start_time = '.time().', correct_number = "'.rand(0,9).''.rand(0,9).''.rand(0,9).''.rand(0,9).'"'); bcz if i do rand(0,9999) it will show a number below 1000 like "251" or something, they have to be 4 numbers Quote Link to comment https://forums.phpfreaks.com/topic/268488-whats-the-difference-between/#findComment-1378807 Share on other sites More sharing options...
kicken Posted September 18, 2012 Share Posted September 18, 2012 bcz if i do rand(0,9999) it will show a number below 1000 like "251" or something, they have to be 4 numbers You could just zero-pad your rand call to make it 4 digits long. $num = str_pad(rand(0, 9999), 4, '0', STR_PAD_LEFT); Quote Link to comment https://forums.phpfreaks.com/topic/268488-whats-the-difference-between/#findComment-1378820 Share on other sites More sharing options...
scootstah Posted September 18, 2012 Share Posted September 18, 2012 bcz if i do rand(0,9999) it will show a number below 1000 like "251" or something, they have to be 4 numbers You could just zero-pad your rand call to make it 4 digits long. $num = str_pad(rand(0, 9999), 4, '0', STR_PAD_LEFT); Or just use rand(1000,9999); Quote Link to comment https://forums.phpfreaks.com/topic/268488-whats-the-difference-between/#findComment-1378881 Share on other sites More sharing options...
Psycho Posted September 18, 2012 Share Posted September 18, 2012 @monkuar You really aren't making any sense. You state actually 0000,9999 bcz i cant have numbers below 1000 and then state you will use $db->query('UPDATE daily4 set start_time = '.time().', correct_number = "'.rand(0,9).''.rand(0,9).''.rand(0,9).''.rand(0,9).'"'); which will result in values < 1000 I think kicken correctly deduced that what you REALLY want is a number between 0 and 9999 which is always padded to four digits. So, the number 41 will be 0041. If that is what you want, then the solution he provided is what you should probably use. The more time you take into specifying your requirements as accurately and completely as possible will greatly increase your chances of getting the correct answer the first time. Quote Link to comment https://forums.phpfreaks.com/topic/268488-whats-the-difference-between/#findComment-1378893 Share on other sites More sharing options...
scootstah Posted September 18, 2012 Share Posted September 18, 2012 I think kicken correctly deduced that what you REALLY want is a number between 0 and 9999 which is always padded to four digits. So, the number 41 will be 0041. If that is what you want, then the solution he provided is what you should probably use. If that's the case, he should just use UNSIGNED_ZEROFILL on the column and it will happen automatically. Quote Link to comment https://forums.phpfreaks.com/topic/268488-whats-the-difference-between/#findComment-1378904 Share on other sites More sharing options...
Monkuar Posted September 18, 2012 Author Share Posted September 18, 2012 @monkuar You really aren't making any sense. You state actually 0000,9999 bcz i cant have numbers below 1000 and then state you will use $db->query('UPDATE daily4 set start_time = '.time().', correct_number = "'.rand(0,9).''.rand(0,9).''.rand(0,9).''.rand(0,9).'"'); which will result in values < 1000 I think kicken correctly deduced that what you REALLY want is a number between 0 and 9999 which is always padded to four digits. So, the number 41 will be 0041. If that is what you want, then the solution he provided is what you should probably use. The more time you take into specifying your requirements as accurately and completely as possible will greatly increase your chances of getting the correct answer the first time. how would .rand(0,9).''.rand(0,9).''.rand(0,9).''.rand(0,9).' result in a number less than 1000? I thought if becomes 0, it will just show 0, so what if all were 0 it would show 0000 right? or maybe if 1 became 5 and all became zero, it would do 0050 ? or would I need to set them all to a different variable and do it like this $var1 = rand(0,9); $var2 = rand(0,9); $var3 = rand(0,9); $var4 = rand(0,9); and then do $var1.$var2.$var3.$var4 right? numbers can only be 0001 to 9999 nothing more or less Edit: Actually I just am using this: $rand1 = rand(0,9); $rand2 = rand(0,9); $rand3 = rand(0,9); $rand4 = rand(0,9); $db->query('UPDATE daily4 set start_time = '.time().', correct_number = "'.$rand1.''.$rand2.''.$rand3.''.$rand4.'"'); works fine Quote Link to comment https://forums.phpfreaks.com/topic/268488-whats-the-difference-between/#findComment-1378912 Share on other sites More sharing options...
Jessica Posted September 18, 2012 Share Posted September 18, 2012 0000 is LESS THAN 1000. You're talking about the number of digits, not the value of the number, but you keep saying things that refer to the value. Quote Link to comment https://forums.phpfreaks.com/topic/268488-whats-the-difference-between/#findComment-1378916 Share on other sites More sharing options...
Psycho Posted September 18, 2012 Share Posted September 18, 2012 how would .rand(0,9).''.rand(0,9).''.rand(0,9).''.rand(0,9).' result in a number less than 1000? I thought if becomes 0, it will just show 0, so what if all were 0 it would show 0000 right? Really?! You must be using some type of "new" math. Edit: Actually I just am using this: $rand1 = rand(0,9); $rand2 = rand(0,9); $rand3 = rand(0,9); $rand4 = rand(0,9); $db->query('UPDATE daily4 set start_time = '.time().', correct_number = "'.$rand1.''.$rand2.''.$rand3.''.$rand4.'"'); works fine Sure it does, but I could write an even more complicated solution that takes hundreds of lines to produce the same results. Kicken provided the solution you should be using. Quote Link to comment https://forums.phpfreaks.com/topic/268488-whats-the-difference-between/#findComment-1378924 Share on other sites More sharing options...
Monkuar Posted September 18, 2012 Author Share Posted September 18, 2012 Okay, i'll use kickens then, does look nicer. topic solved... I just don't really like to use code that i don't know much about, so i had to read the manual documention for str_pad before using, but i've switched my $var1.$var2.$var3.$var4 crap out, thanks. now it looks nice and spiffy Quote Link to comment https://forums.phpfreaks.com/topic/268488-whats-the-difference-between/#findComment-1378928 Share on other sites More sharing options...
scootstah Posted September 18, 2012 Share Posted September 18, 2012 Once again, you can use ZEROFILL on an integer column in MySQL to pad the 0's for you. So if you define num INT(4) ZEROFILL and input "1", it will come back as "0001". Quote Link to comment https://forums.phpfreaks.com/topic/268488-whats-the-difference-between/#findComment-1379043 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.