?
Solved

Php - random with 4 digits

Posted on 2012-09-04
6
Medium Priority
?
513 Views
Last Modified: 2012-09-04
Hi E's, can you show me how I do a random with four digits [0-3]?
md5 can do that?

The best regards, JC
0
Comment
Question by:Pedro Chagas
  • 3
  • 2
6 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 640 total points
ID: 38363380
Why not just use the rand function?

e.g.

$random = rand(1000, 9999);

Open in new window

0
 
LVL 35

Expert Comment

by:gr8gonzo
ID: 38363455
Try this:
<?php

// Generate a random number
$randomNumber = str_pad(rand(0,10000),4,"0",STR_PAD_LEFT);

// Generate a random password and append the random number
$newPassword = randomPasswordGenerator().$randomNumber;

// Password generator
function randomPasswordGenerator($minimumlength = 8)
{
	$newPassword = "";
	$randChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()_+-=.,?";
	for($i = 0; $i < 16; $i++)
	{
		$newPassword .= $randChars[mt_rand(0,strlen($randChars))];
	}
	return $newPassword;
}
?>

Open in new window

0
 
LVL 3

Author Closing Comment

by:Pedro Chagas
ID: 38364989
Hi @gr8gonzo, you read my question?
Thanks for both.

The best regards, JC
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 35

Expert Comment

by:gr8gonzo
ID: 38365221
Hi JC,

Yes, I did. You said "random WITH four digits". When you say "with", it sounds like you're saying "a random" AND "four digits".  I assumed you meant "a random string" + 4 digits. That is what my code provides.
0
 
LVL 3

Author Comment

by:Pedro Chagas
ID: 38365284
My apologizes. English language is not my native language. I try write the best I can.
The best regards, JC
0
 
LVL 35

Expert Comment

by:gr8gonzo
ID: 38365365
It's no problem. I was just explaining why I answered the way I did, because you had asked if I had read the question. :)
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses
Course of the Month15 days, 16 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question