Solved

Generate a file of Random Numbers

Posted on 2009-07-14
6
235 Views
Last Modified: 2012-05-07
I need to generate a file of 500 random domain names. The domain should be made in this format

a647567-ass.sc3.domain.com

The first letter is random and so is the number following it, the rest is static. The generated domains should be written to a file in the same directory.

Can anyone help?
0
Comment
Question by:MayoorPatel
  • 2
  • 2
  • 2
6 Comments
 
LVL 16

Assisted Solution

by:alain34
alain34 earned 200 total points
ID: 24851407
Hello MayoorPatel,

THe following should do what you want.

<?

$fileContent = "";

for ($i =0 ; $i < 500 ; $i++) {
   $letter = chr(rand(77,122));
   $number = "";      
   for ($j =0 ; $j < 6 ; $j++) {
           $number .= rand(0,9);           
      }
   $fileContent .= $letter . $number . "-ass.sc3.domain.com\r\n";  
   }

$handle = fopen("randomDomain.txt", "w");
fwrite($handle,$fileContent);
fclose($handle);

exit;  
?>

Regards,

alain34
0
 
LVL 18

Accepted Solution

by:
Hube02 earned 300 total points
ID: 24851459
Try this:
<?php

  

  if ($file = fopen('random_domains.txt', 'w')) {

    for ($i=0; $i<500; $i++) {

      $letter = chr(mt_rand(97, 122));

      $number = str_pad(strval(mt_rand(0, 999999)), 6, '000000', STR_PAD_LEFT);

      $domain = $letter.$number.'-ass.sc3.domain.com'."\r\n";

      fwrite($file, $domain);

    }

    fclose($file);

  } else {

    'unable to open file for writing';

  }

  

?>

Open in new window

0
 
LVL 1

Author Comment

by:MayoorPatel
ID: 24851461
Looks good Alain however its generating some characters which are not letters

m063722-ass.sc3.domain.com
h322375-ass.sc3.domain.com
o311240-ass.sc3.domain.com
d474816-ass.sc3.domain.com
k553627-ass.sc3.domain.com
\599811-ass.sc3.domain.com
W879291-ass.sc3.domain.com
e310784-ass.sc3.domain.com
i912476-ass.sc3.domain.com
S944534-ass.sc3.domain.com
_566436-ass.sc3.domain.com
m272682-ass.sc3.domain.com
a670593-ass.sc3.domain.com
P602647-ass.sc3.domain.com
T715371-ass.sc3.domain.com
p174357-ass.sc3.domain.com
N394975-ass.sc3.domain.com
e686288-ass.sc3.domain.com
w350166-ass.sc3.domain.com
a482566-ass.sc3.domain.com
w139434-ass.sc3.domain.com
a199786-ass.sc3.domain.com
P740001-ass.sc3.domain.com
T776168-ass.sc3.domain.com
m346485-ass.sc3.domain.com
y204394-ass.sc3.domain.com
T802542-ass.sc3.domain.com
h537213-ass.sc3.domain.com
^710167-ass.sc3.domain.com
g535739-ass.sc3.domain.com
U343145-ass.sc3.domain.com
m982420-ass.sc3.domain.com
l340161-ass.sc3.domain.com
W288713-ass.sc3.domain.com

The first letter must only be a-z
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 18

Expert Comment

by:Hube02
ID: 24851475
@alain34: Sorry, must have been still writing when you made your post.

A slightly different solution, but the same result
0
 
LVL 1

Author Closing Comment

by:MayoorPatel
ID: 31603329
Cheers guys
0
 
LVL 16

Expert Comment

by:alain34
ID: 24851548
Yes, it is a misspelled in my code
rand(77 should be replaced by
rand(97
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

746 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now