/    Sign up×
Community /Pin to ProfileBookmark

which one is better?

Which one do you prefer among the two?

[code=php]function genRandomNo($length = 5){
$string = substr(md5(uniqid(rand(), true)), 0, $length);
return $string;
}[/code]

Vs

[code=php]function genRandomNo($length = 5){
$string = substr(md5(microtime() * mktime()), 0, $length);
return $string;
}[/code]

also you can suggest alternatives.

Thanks

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@MindzaiMar 11.2010 — depends what you want it to do! If you just want a random number I'd use mt_rand() and be done with it. If you want a random string i'd use uniqid(). But unless you tell us what it's far it's hard to offer advice.
Copy linkTweet thisAlerts:
@dk_zero-coolMar 11.2010 — For a random string, I would go with something like the first one. Using time functions does not generate anything random.
Copy linkTweet thisAlerts:
@scragarMar 11.2010 — To be perfectly fair neither produces a random string(although the first one would have a random part). If you want a random number just generate a random number and be done with it:
[code=php]function genRandomNo($length = 5){
$string='';
for($i = $length; $i; $i--)
$string .= dechex(rand(0,15));
return $string;
}[/code]
×

Success!

Help @PHPycho spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...