Solved

Generate a file of Random Numbers

Posted on 2009-07-14
6
249 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
WordPress Tutorial 1: Installation & Setup

WordPress is a very popular option for running your web site and can be used to get your content online quickly for the world to see. This guide will walk you through installing the WordPress server software and the initial setup process.

 
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

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

615 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