Solved

Generate a file of Random Numbers

Posted on 2009-07-14
6
245 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
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 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Suggested Solutions

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

710 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