[1+1=2]

OneAndOneIs2

« I. . . I think it's. . . dead!Why deleting just isn't enough »

Thu, Jan 04, 2007

[Icon][Icon]Coding a random filesystem diagram

• Post categories: Omni, Programming

You may have noticed that it took 676 random characters to fill in the diagram of a shredded filesystem in my last post.

Rather than hit my keyboard until the correct amount of randomness had taken place, and then correctly space and HTMLify it, I decided to take the opportunity to have a play with random-number generation in C.

I wanted the ASCII characters from 33 to 126, as these are the letters, numbers, and punctuation. So I needed to generate a range of 93 numbers from 0 to 92 and add them to 33 to make this work.

I also needed to "seed" the rand() function as is usual by feeding it the exact time. Otherwise you always get the same random number (go figure).

Here, purely for academic interest, is the code I used to generate the complete random filesystem:

Follow up:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
        int randchar;
        int i, j;
        time_t t;
        srand(time(&t));

        /* Generate the first row with the alphabet & HTML */
        printf("<pre><span class=\"red\">   ");
        for (i = 0; i != 26; ++i)
        {
                putchar('a' + i);
                putchar(' ');
        }
        printf("</span>\n\n");

        /* Generate the random data & HTML etc. */
        for (i = 0; i != 26; ++i)
        {
                printf("<span class=\"red\">");
                putchar('a' + i);
                printf("</span>  ");
                /* Randomly print 26 random characters from 33 to 126
                 * with spaces in-between */
                for (j = 0; j != 26; ++j)
                {
                        putchar(randchar = (rand() % 93)+33);
                        putchar(' ');
                }
                if (i == 25)
                        printf("</pre>");
                putchar('\n');
        }
        return 0;
}

1 comment

Alison
Comment from: Alison [Visitor] · http://www.creativehedgehog.com
heh. nice.
05/01/07 @ 06:27

Leave a comment


Your email address will not be revealed on this site.

Your URL will be displayed.
(Line breaks become <br />)
(Name, email & website)
(Allow users to contact you through a message form (your email will not be revealed.)
This is a captcha-picture. It is used to prevent mass-access by robots.
Please enter the characters from the image above. (case insensitive)
 

[Links][icon] My links

[Icon][Icon] Hmm.. new look for twitter? I hope it gets less "Ick! Change! Put it back!" nonsense than Facebook..
08/02/12

[Icon][Icon] Facebook Syndication Error
09/02/12

[Icon][Icon] I last listened to:
Johann Pachelbel - Canon in D major

[Icon][Icon] Most recent photo:
Submersible houseboat

[Icon][Icon]About Me

[Icon][Icon]About this blog

[Icon][Icon]My LQ profile

[Icon][Icon]My /. profile

[Icon][Icon]My Wishlist

[Icon]MyCommerce

[FSF Associate Member]


February 2012
Mon Tue Wed Thu Fri Sat Sun
 << <   > >>
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29        

Search

User tools

XML Feeds

eXTReMe Tracker

Valid XHTML 1.0 Transitional

Valid CSS!

[Valid RSS feed]

blog engine