NerdConcepts Posted July 21, 2007 Share Posted July 21, 2007 I am trying to generate a random 6 digital number. All I know how to do is " rand(1,10); " and stuff like that, but that won't work. The number I am trying to generate needs to use 0-9 and come up with 6 digits every time. I'm sure this is something I should already know, but the whole MD5 stuff is WAY to large of a number for an in-house RA number Only idea I have is to run the "rand(0,9)" (if that is possible, haven't tried it) 6 different times with variable names and then put them together, but there has to be a better way, right? Quote Link to comment https://forums.phpfreaks.com/topic/61132-solved-generating-6-random-digits/ Share on other sites More sharing options...
pedrobcabral Posted July 21, 2007 Share Posted July 21, 2007 <?php $x=1; while ($x <= 6) { $random .= rand(0,9); $x++; } echo $random; ?> Quote Link to comment https://forums.phpfreaks.com/topic/61132-solved-generating-6-random-digits/#findComment-304210 Share on other sites More sharing options...
Psycho Posted July 21, 2007 Share Posted July 21, 2007 This single line generates a random number from 0 to 999999 and then pads with zeros if needed: $var = sprintf("%06s", rand(0,999999)); Quote Link to comment https://forums.phpfreaks.com/topic/61132-solved-generating-6-random-digits/#findComment-304213 Share on other sites More sharing options...
NerdConcepts Posted July 21, 2007 Author Share Posted July 21, 2007 Thanks a bunch. Not exactly sure how it works, but it does. I'll have to read up on the command to get more information on it. Quote Link to comment https://forums.phpfreaks.com/topic/61132-solved-generating-6-random-digits/#findComment-304248 Share on other sites More sharing options...
Albatross Posted July 23, 2007 Share Posted July 23, 2007 I know the sign says solved, but here's the one I just used yesterday and it seems to play nicely. <?php echo rand(0, 999999); ?> Quote Link to comment https://forums.phpfreaks.com/topic/61132-solved-generating-6-random-digits/#findComment-305036 Share on other sites More sharing options...
Psycho Posted July 23, 2007 Share Posted July 23, 2007 I know the sign says solved, but here's the one I just used yesterday and it seems to play nicely. <?php echo rand(0, 999999); ?> No, it does not meet the parameters that the poster asked for. Namely, if the value of rand is less than 100,000 the value should be padded with zeros to make the value exactly 6 digits. You may not have found that problem with your code because you will only get a value that requires padding about 10% of the time. Try running it several times and you will see the problem. Quote Link to comment https://forums.phpfreaks.com/topic/61132-solved-generating-6-random-digits/#findComment-305074 Share on other sites More sharing options...
Albatross Posted July 23, 2007 Share Posted July 23, 2007 just checked mine n you're right, I used 1000000000-9999999999 and didn't start from zero. Was on the right track but wrong train. Quote Link to comment https://forums.phpfreaks.com/topic/61132-solved-generating-6-random-digits/#findComment-305086 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.