Solved

Generate a file of Random Numbers

Posted on 2009-07-14
6
241 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
Technology Partners: 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 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
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.

679 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