Jump to content

Whats the difference between..


Monkuar

Recommended Posts

<?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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

@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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

@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 :)

 

 

Link to comment
Share on other sites

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?

:confused: 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.

Link to comment
Share on other sites

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 :D

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.