Solved

Generate a file of Random Numbers

Posted on 2009-07-14
6
238 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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

809 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