Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

random number with a fixed length

Status
Not open for further replies.
Use this:
int rand ( [int min, int max])

Pass in the min and max values.

So, you'd do this:

$random = rand (10000000, 99999999)
 
I doubt your VBScript example would generate a number of fixed length. What would happen if the random function generates 4?


You could, I suppose, left-pad with zeroes:

$number = sprintf ('%08d', mt_rand(1, 99999999));

Or generate them a digit at a time:

$number = '';
for ($counter = 0; $counter < 8; $counter++)
{
$number .= mt_rand(0, 9);
}



Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Which is what you want? Or not?

The solution I gave you works fine, but if you'd like your numbers to have the potential of any number of leading 0's, then sleipnir214's solution is the way to go.

My solution will give you numbers like:
94357865
32749035
95420163
12345678

While sleipnir214's will give you:
123456789
987654321
000043521
008996353

So, the choice is yours.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top