| « I. . . I think it's. . . dead! | Why deleting just isn't enough » |
Thu, Jan 04, 2007
![[Icon]](rsc/img/chain_link.gif)
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;
} ![[Links]](http://geekblog.oneandoneis2.org/skins/112/rsc/img/chain_link.gif)
Hmm.. new look for twitter? I hope it gets less "Ick! Change! Put it back!" nonsense than Facebook..
08/02/12
Facebook Syndication Error
09/02/12
![]()
I last listened to:
Johann Pachelbel - Canon in D major
Most recent photo:
Submersible houseboat