PDA

View Full Version : More Random Displays


Little_C
11-11-2003, 05:19 PM
First contest for me; Yes! Plus I haven't posted in about four months.

Contest: Write a program (any language) that displays 1000 random letters. can be upper- or lower-case. Also include a mechanism to measure the time elapsed. I'm not looking for neatness, etc. It's all about speed! Include the output.

Here's a MetaL Basic version:


cls
randomize timer
t=timer
do until x = 1000
reset:
Zeus = random(90)
if Zeus < 65 then
goto reset
end if
if Zeus > 90 then
goto reset
end if
print chr$(Zeus);
x=x+1
loop
print " "
print "Took ";
print timer-t;
print " seconds to display ";
print x;
print " random letters"
end


Here's the output:

JMOICHKCKWQXOFMMIJPBMNGNDEALAKKBAAMVJHHQSGHJMJMFUDONKOCWGTRXKIXYOEDWEWRPYUQSLNAMOVUPYPZTRJHBEFZGVUTR AWUUYHFKRROOTTDOIYKPVTIYBVAIPPAXVFYUTHLEDBEOOTTJSSEQDCTPOBXLABAPYAELHWIAERKSNPBFGRHDHDMDJQACEFTSMRHK BGYGHYCRTORYIDCFBORQKHNSBVLQKHBQTVBYRYEZVBUPSROPIUZOHSWQFDLHGHUYZDLRSAJKOVFLMRPIAHHZSAORGQXKLLXFEOZF COUPDUTFXOZQYZJUCLSDFMZXAGQPJXJJKJQGTYZJPJSVWENSVVCWUJUXNIJHXFKOEMZEGTMFASQYACFSGFCMHRXILSBOKLRINWOP FSIWKHKWJJEKXFNYZHHFSAFCMWGORFBZABMMCSIHBVQKAJCEYXBVGNYZWTCGHXSQSDPKLTDJVVIEDRVRCAYRPSIGYJLGBYWLJDPO GJYUXVFMKCIFAECJPBDSWZWQGLOBSLLFHIGGBVCVTWMWTKGFQNIAVZXCJSRDZNWJKZDMJCHSBLJPFCLHYMCPLNJRRRIXDUBGIJSB PVYUKVWYAEILXWGCETTZFVWVWURWPVXKBXMJMKJGFINCBHPXHZOKVTAMQGPVYELZUSLQRKNKHAEOLKPNJAUVXLBRJDIJJQUKDPYC GJNFYLGVOFVYQQKBRLDZRDUQVMZEKKYWKILEMVTHPGVKFTBUYMINJLQLNDFFLWPGEQYIZFXWUNOOMXYMQJEGCPAJXYNRXCZFETRN NRWOJUSHWVIITERAIRHELZAHZIYECJOZCJQHZUACHTPQSKWWEAXLRLPARJNRAEBQKLGNMAGQMGPBLLDQHIJFAVPMEWJDFATJQMBS OOCPDFXVNWAKRZZOKCVHGXMYRBBYSVKJYSBKJLZUSAYHGGTTJTTVYRSIMXIYEBUHXQMEHWPUXWTWTXALLZPYHWOZLBKFBIXETQBP
Took 0.011777 seconds to display 1000 random letters

php_brian
11-11-2003, 06:25 PM
I wrote it in PHP. Pretty quick.


<?php

function timer() {
list($usec,$sec) = explode(" ",microtime());
return ((float)$usec + (float)$sec);
}

srand((double)microtime() * 1234567);

$start = timer();

$letters = range('A','Z');
$count = count($letters);

for($i = 0; $i < 1000; $i++) {
echo $letters[rand(0,$count)];
}

$end = timer();

echo "

\n";
echo "executed in " . ($end - $start) . " seconds.";

?>


Averages 0.004 seconds. Check http://www.brosner.com/random.php for the runtime.

MJPhill
11-11-2003, 08:21 PM
Here's my java:

import java.util.*;

public class RandLetter {

public static void main(String[] args) {
Date BeginTime = new Date();
Random Rand = new Random();
String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String out = "";
for(int i = 0; i < 1000; i++) {
int place = Rand.nextInt(26);
out += alphabet.substring(place, place+1);
}
System.out.println(out);
Date EndTime = new Date();
long Duration = EndTime.getTime()-BeginTime.getTime();
System.out.println();
System.out.println("Program completed in: " + Duration + " milliseconds");
}

}

It took about 31 milliseconds to complete on my computer, even though that is irrelavent.

Tygur
11-13-2003, 01:06 AM
This contest seems a bit flawed. You should be generating far more than 1000 random numbers. 1000 just doesn't take long enough. When you're timing such short durations, results aren't very accurate.

Also, the performance of your code is limited by the performance of whatever random number generator you're using. It looks like so far, everyone who posted used whatever random number generator came with their language of choice. To me, it looks like all you're really doing is testing the speed of each random number generator that comes with each language. There's no real skill involved.

Finally, how about the "randomness" of the numbers being generated by your random number generators? What if I made my own that didn't do quite as good a job as other random number generators, but was ten times faster? Would my code win, then? What if it was so bad that you'd find patterns if you looked close enough, or it obviously favored certain letters?

php_brian
11-13-2003, 04:37 PM
Here's an idea for this contest as a revision. Give us x amount of letters that were randomly generated or allow us to randomly generate letters and then make the program try to find a pattern. It would be better if the letters were given and there was a pattern, but it was very hard to find. I don't know :)

Little_C
11-13-2003, 06:46 PM
Originally posted by Tygur
This contest seems a bit flawed. You should be generating far more than 1000 random numbers. 1000 just doesn't take long enough. When you're timing such short durations, results aren't very accurate.

Also, the performance of your code is limited by the performance of whatever random number generator you're using. It looks like so far, everyone who posted used whatever random number generator came with their language of choice. To me, it looks like all you're really doing is testing the speed of each random number generator that comes with each language. There's no real skill involved.

Finally, how about the "randomness" of the numbers being generated by your random number generators? What if I made my own that didn't do quite as good a job as other random number generators, but was ten times faster? Would my code win, then? What if it was so bad that you'd find patterns if you looked close enough, or it obviously favored certain letters?

The contest is to determine whose entry creates the random letters in the shortest ammount of time. If it obviosly favored certain letters/letter combanations, the problem is not mine, but yours. I don't really care about fraud. It's just for fun. You could make a program that just print "ABDASO" over and over again. I would just ignore obviously fraudulent entries.

And, on a final note, I didn't "[use] whatever random number generator came with [my] language of choice". MetaL's standard random letter generation system is far different from mine. I just picked a random number from 65 to 90, and got the ASCII equivalent of that. If I wanted to, I could use a "conventional" system, but, it's all just for fun.

And, If I didn't answer your question, I probably didn't read it close enough. If you are really intent on having it changed, say so. I'll make the number 500,000, or something.

Tygur
11-13-2003, 07:20 PM
I don't think you read my post well enough. I was referring to the random number generator that is used when coming up with the random letters. It looks like you each got your numbers from whatever generator came from the language. I was supposing what if I made my own random number generator that was faster than anything else, but didn't make such random results (the performance of random number generators can vary).

MJPhill
11-13-2003, 07:58 PM
There is a slight difference between the approach that php_brian and myself took and the approach Little_C took. With our methods, a letter will be returned with every iteration of the loop. However with Little_C's method, a letter is returned only if a number within the range of 65 and 90 is chosen. So theoretically his program could take quite a bit longer if the process of choosing numbers is truely random, depending on the amount of numbers lower than 65 that are chosen.

Tygur
11-14-2003, 02:56 AM
That is true, but what do you think makes a bigger difference: how you generate the random number or how you take that number and chose a letter?

What I'm saying is that the speed of whatever random number generator you're using does matter, and so far, everyone is just using whatever comes with their language of choice. I could be wrong, but my guess is that which generator is used has a much bigger bearing than how you use the number to pick a letter, unless the method used is excessively inefficient. Little_C's code is probably the slowest, but what about the other two? Coming up with a random number isn't really the quickest and simplest thing to do, and there are many different approaches that might be implemented.

MJPhill
11-14-2003, 03:45 AM
Franklu, I do not have enough experience to deal with this situation I think, DNAUnion you are the man and have the code to end all code.

php_brian
11-15-2003, 12:07 AM
I would like to point out something...


Contest: Write a program (any language) that displays 1000 random letters. can be upper- or lower-case. Also include a mechanism to measure the time elapsed. I'm not looking for neatness, etc. It's all about speed! Include the output.


Does it mention anything about being restricted to a language's random number generator? Stop complaining and code your new and improved way. Show us the speed, its what it is all about right?

Tygur
11-16-2003, 04:49 AM
Well, you asked for the speed..

My C# code:
using System;

namespace RandomNumbersContestCSharp
{
class RandomNumberTest
{
[System.Runtime.InteropServices.DllImport("KERNEL32.DLL")]
static extern Int32 QueryPerformanceFrequency(ref Int64 lpFrequency);
[System.Runtime.InteropServices.DllImport("KERNEL32.DLL")]
static extern Int32 QueryPerformanceCounter(ref Int64 lpPerformanceCount);

[STAThread]
static void Main(string[] args)
{
Int64 pf=0;
//gets the number of counts per second
int hc = QueryPerformanceCounter(ref pf);
if (hc==0||pf==0) {
//i think this is very rare, actually
Console.WriteLine("Your computer cannot run this code.");
} else {
Int64 startTime=0;
Int64 stopTime=0;
QueryPerformanceCounter(ref startTime);
GenerateLetters(1000);
QueryPerformanceCounter(ref stopTime);
Console.Write("Duration: ");
Console.Write(stopTime-startTime);
Console.Write(" ");
Console.Write(pf);
Console.WriteLine("ths of a second");
Console.Write("Or: ");
Console.Write(Convert.ToDecimal(stopTime-startTime)/Convert.ToDecimal(pf)*1000);
Console.WriteLine(" milliseconds");
}
Console.ReadLine();
}
static uint[] bitmasks = new uint[] {
0x1,0x2,0x4,0x8,
0x10,0x20,0x40,0x80,
0x100,0x200,0x400,0x800,
0x1000,0x2000,0x4000,0x8000,
0x10000,0x20000,0x40000,0x80000,
0x100000,0x200000,0x400000,0x800000,
0x1000000,0x2000000,0x4000000,0x8000000,
0x10000000,0x20000000,0x40000000,0x80000000};
const int mask5 = 0x1f;
const uint lastbit = 0x80000000;
static void GenerateLetters(int count) {
int i;
//seed the generator
uint seed = (uint)Environment.TickCount;
//make sure we don't have that crowd of zeros at one end
int sc = 0;
for (i=30;i>=0;i--) {
if ((bitmasks[i]&seed)!=0) {
sc=i+1;
break;
}
}
seed = (seed<<sc)|seed;
//generate count letters
char[] letters = new char[count];
for (i=0;i<count;i++) {
//rotate the bits
uint lb = (seed&lastbit);
seed = seed << 1;
if (lb!=0)
seed=seed|1;
//switch a bit
seed = seed ^ bitmasks[mask5 & seed];
//pick a letter
uint num = seed & mask5;
if (num>25) num-=25;
//add the letter
letters[i] = System.Convert.ToChar(65+num);
}
//write the output
Console.WriteLine(letters);
}
}
}


Program output, including the amount of time that it took:
GMYQDHOECXPGGGFECXPGFDYRGMZTGMZTGNBUJSFLXPGFDZSUIRLWMYQDGMZSUIRLXPFDYQBGNCXPGFEB
UIRLWNCXPFECXPFEBUJSUIQBGNCXPGGFEBVKVLWMZTHPFDZTGNCWMZSUIQBGMYRGNBVLXODZSUJTGNBV
KVLWNCXPFEBUIQDHPGFEBVLWNBUJSUIQDHPFDYQDGNCWMYRLXPGGGFECXPGFDYQDHOEBUIRLWMYQDGMZ
SFLWNCWNCWMZSUIRGMZTHOEBUIQDGNBVLXPFECXPGFDYQDGMZTHPGFDYRGMZTHPFEBUIQDHPFECXOEBU
IRLXODZTGNBVKUIRLWMYQDHPGGFEBVKVKVKVLWNCWNCXOECWMYRLXPFECXPGFEBVLWNCXODZTHPGGGGG
FDYRLXODZTGNBVLWNBUJTGNBVKVLXOECWNBUJTGMYQBLWNCWNBVKUIQDHOECXOECWMYRGMZSFLXODZTH
OEBVKUJTGNCWNCXODZTGMZTHPGGGGGFECWMYQDHPGGFDYQDHPGFDZTGMYRLWNBUIQDHPGGFEBVKVKVKU
JTGMZTHOECXOEBUJSFLXOEBVLWNCXOECWMZTGMZTHODYQDHOECXPGGFEBVKVKUJTGMZTGMYQDGMYQDGM
ZTGMZTGNBUJSFKVLWMYRGNBUIQDGNCWMYQBGMYQBLXPFEBVLXPFECWMYRGNCXPFDYRGNCWMYQBGMZTGN
BVKUJSUJSUJTGNBUJTHODYRGNCWMZSFLXOECWMZSFLXODYRLWNCWNCXPGFDZTHPFECWNCWMZSFLWMYQD
GNCWNCWMZSUJTGMYRLWNCWMYQDGNCWNBUIRGNBUIQBLWMYQBLXPGGGGFDZTHODYRGMZTHOEBUJTGMYQD
GNCXPGFECXOECWNCWNBVLWNCXPGGGGGGGFEBUIQDHOECXPGFDYQDGNBVLWNBUIQDHOECWNCXODZSFKUI
RLXODYQDHOECWMZSUIRGMZTHODZTGMZSFLXPFEBV
Duration: 91267 358336660117ths of a second
Or: 0.0002546962400391870034046 milliseconds


Like I said, 1000 numbers is really too few. The code above ran so quick, I had to use a high performance counter to time it.

php_brian
11-16-2003, 02:21 PM
Very nice!

Tygur
11-17-2003, 04:18 AM
I knew something was wrong. It just didn't look right. Due to an error in the timing code, the code I just posted actually takes longer than it says it does. I converted it to C++ and fixed the timing mistake, but it turns out to be about as fast as the php code.

After some further testing, it turns out that most of the time is actually spent displaying the output rather than simply generating the numbers. In fact, if I comment out the lines that display the output, my code takes well under a millisecond (I'm sure this time). You can test that out yourself. This gave the php code an advantage because all the displaying it needed was to store the output in a buffer.

Here is the now-fixed code, converted to C++. Actually, since I'm not using classes, wouldn't this technically be C code? I don't know all the details of the differences between the two, but here's the fixed code:
#include <windows.h>
#include <stdlib.h>

void GenerateLetters(int count);
void Write(char* line);
void Write(char* line,int charcount);
void Pause();

int main(int argc, char* argv[])
{
__int64 pf;
__int64 st;
__int64 et;
QueryPerformanceFrequency((LARGE_INTEGER*)&pf);
QueryPerformanceCounter((LARGE_INTEGER*)&st);
GenerateLetters(1000);
QueryPerformanceCounter((LARGE_INTEGER*)&et);

double ticks = et-st;
double secs = ticks/(double)pf;
double milsecs = secs*1000;
double micsecs = milsecs*1000;

int decimal,sign;
char* buffer = _fcvt(milsecs,2,&decimal,&sign);
Write("\nDuration: ");
Write(buffer,decimal);
Write(".");
Write(buffer+decimal);
Write(" milliseconds\n");

buffer = _fcvt(micsecs,2,&decimal,&sign);
Write("Alt Duration: ");
Write(buffer,decimal);
Write(".");
Write(buffer+decimal);
Write(" microseconds\n");

Write("1000 milliseconds = 1 second\n1000 microseconds = 1 millisecond\n");

Pause();
return 0;
}

void Write(char* line) {
Write(line,strlen(line));
}
void Write(char* line, int charcount) {
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
DWORD written;
WriteConsoleA(hOut,line,charcount,&written,0);
}
void Pause() {
Write("\nPress ENTER to quit.");
HANDLE hIn = GetStdHandle(STD_INPUT_HANDLE);
ReadConsoleA(hIn,0,0,0,0);
}

const unsigned int bitmasks[32] = {
0x1,0x2,0x4,0x8,
0x10,0x20,0x40,0x80,
0x100,0x200,0x400,0x800,
0x1000,0x2000,0x4000,0x8000,
0x10000,0x20000,0x40000,0x80000,
0x100000,0x200000,0x400000,0x800000,
0x1000000,0x2000000,0x4000000,0x8000000,
0x10000000,0x20000000,0x40000000,0x80000000};
const int mask5 = 0x1f;
const unsigned int lastbit = 0x80000000;
void GenerateLetters(int count) {
int i;
//seed the generator
unsigned int seed = GetTickCount();
//make sure we don't have that crowd of zeros at one end
int sc = 0;
for (i=30;i>=0;i--) {
if ((bitmasks[i]&seed)!=0) {
sc=i+1;
break;
}
}
seed = (seed<<sc)|seed;
//generate count letters
char* letters = new char[count];
for (i=0;i<count;i++) {
//rotate the bits
unsigned int lb = (seed&lastbit);
seed = seed << 1;
if (lb!=0) seed=seed|1;
//switch a bit
seed = seed ^ bitmasks[mask5 & seed];
//pick a letter
unsigned int num = seed & mask5;
if (num>25) num-=25;
//add the letter
letters[i] = 65+num;
}
//write the output
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
WriteConsoleA(hOut,letters,count,0,0);
}

Here is some sample output:
OECXODZSUJSUJTHODZSFLWNBVLXPFDYQBLXOEBUJSFKUIRLXOECXOEBUJTHPGGGGGFDYRGNCWNBUIRL
NCWNBVLXPGGFDYRLWMYQBLWNBUIQBGMYQDGMYRLWNCWNBUIRLXODZTGNBVLXODZSFKVLXODYRLWNBUJ
GNCWMZSFLWNBUJSUJTHOECXPGFDYRGNBUJSFLXODYQDGNCXOEBUJTHPGFDZTHPGFDZSFKUIRGNBUJTH
ECWMZSFKUIRGNCXODYRLWMZSUJSUJTGNCXODZSFLXOECXOEBVKUJSUJTHPFDZTHPGGFECWMYRLWNCXP
DYQDGMYRLWNBVLXOEBVLXODZTGNCXOEBVKUJSUIQBGNCXODZSUJSFLXPGGGFEBVLXODYQBLXOEBUIRG
YQDHPGFDZTGNBUJSFLXOEBUIQBGMYQDGMZSFLXPFEBUJSFKVKVLWMYRLXPFDYRGNBUJTHOECXPGFDZS
KUIQBGNBVKVKUIQDGMZTHPFECXODYQDHPGFECWNBVLWMZTHODZTHPGGFECXPFEBVKUIQBLXPGFECXPG
EBVLXODZSFLXOEBVLWNBVLWMYRGMZTGMZSFKUIQBLXPFDYQDHODZTGMYQBLXPGFEBVKVLXPGGGGGFEB
IQBLWMYQDGNBVLWNBVKVLXOECWMZTHODZSUIQBGMZSFKVLXPGFDYQDGMZSUIRLXPFEBUJSUIQBLWNCX
EBVLXOEBUJTHPFEBUJTGMZSFLXPFECXODYRLWMZTGMZSFLWMYQBGNCWNCXODZTHPFECXPFECWNCWMZS
LXODYRGNCXPFDZTHOEBVLXPGFEBUJTGNBVLWNCXPFDYQDHPFDYQBGNCXODYQDGMYRLXOECWNCWMZTGN
XOEBVLXODZTHODYQBLXPGFEBVKVKVLWMZSFLXOEBVKVLWMZSFKUJTGMZSUJSFLWNBUJTHOECWMZSFLX
FDYRLWMYQBGMZTGNCWNBVLXOECXOECWNBVKUJTHO
Duration: 3.47 milliseconds
Alt Duration: 3466.64 microseconds
1000 milliseconds = 1 second
1000 microseconds = 1 millisecond

Press ENTER to quit.

Tygur
11-19-2003, 04:25 AM
Ok, I changed the C++ code so it uses puts() to display its output, rather than the WriteConsole Win32 API function. The time on my computer is now under a millisecond. Feel free to time it on your own pc's:
#include <windows.h>
#include <stdio.h>

void GenerateLetters(int count);
void Write(char* line);
void Write(char* line,int charcount);
void Pause();

int main(int argc, char* argv[])
{
__int64 pf;
__int64 st;
__int64 et;
QueryPerformanceFrequency((LARGE_INTEGER*)&pf);
QueryPerformanceCounter((LARGE_INTEGER*)&st);
GenerateLetters(1000);
QueryPerformanceCounter((LARGE_INTEGER*)&et);

double ticks = et-st;
double secs = ticks/(double)pf;
double milsecs = secs*1000;
double micsecs = milsecs*1000;

int decimal,sign;
char* buffer = _fcvt(milsecs,2,&decimal,&sign);
Write("\nDuration: ");
Write(buffer,decimal);
Write(".");
Write(buffer+decimal);
Write(" milliseconds\n");

buffer = _fcvt(micsecs,2,&decimal,&sign);
Write("Alt Duration: ");
Write(buffer,decimal);
Write(".");
Write(buffer+decimal);
Write(" microseconds\n");

Write("1000 milliseconds = 1 second\n1000 microseconds = 1 millisecond\n");

Pause();
return 0;
}

void Write(char* line) {
Write(line,strlen(line));
}
void Write(char* line, int charcount) {
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
DWORD written;
WriteConsoleA(hOut,line,charcount,&written,0);
}
void Pause() {
Write("\nPress ENTER to quit.");
HANDLE hIn = GetStdHandle(STD_INPUT_HANDLE);
ReadConsoleA(hIn,0,0,0,0);
}

const unsigned int bitmasks[32] = {
0x1,0x2,0x4,0x8,
0x10,0x20,0x40,0x80,
0x100,0x200,0x400,0x800,
0x1000,0x2000,0x4000,0x8000,
0x10000,0x20000,0x40000,0x80000,
0x100000,0x200000,0x400000,0x800000,
0x1000000,0x2000000,0x4000000,0x8000000,
0x10000000,0x20000000,0x40000000,0x80000000};
const int mask5 = 0x1f;
const unsigned int lastbit = 0x80000000;
void GenerateLetters(int count) {
int i;
//seed the generator
unsigned int seed = GetTickCount();
//make sure we don't have that crowd of zeros at one end
int sc = 0;
for (i=30;i>=0;i--) {
if ((bitmasks[i]&seed)!=0) {
sc=i+1;
break;
}
}
seed = (seed<<sc)|seed;
//generate count letters
char* letters = new char[count+1];
letters[count]=0;
for (i=0;i<count;i++) {
//rotate the bits
unsigned int lb = (seed&lastbit);
seed = seed << 1;
if (lb!=0) seed=seed|1;
//switch a bit
seed = seed ^ bitmasks[mask5 & seed];
//pick a letter
unsigned int num = seed & mask5;
if (num>25) num-=25;
//add the letter
letters[i] = 65+num;
}
//write the output
puts(letters);
//cleanup
delete letters;
}


Output:
BGMZSFKUJTGNBUIQBLWMZSUJSFLXODZTGNBUIQBLWNBVLWNBUJSUJSFKUJSFLWNCXPFEBUIQBGNBVLXP
GGFDYQBGMZSFKVKVLXPGGFDYRGMYQDHOECXPGGFECXPFECXOEBVKUIQBLWMZTGNBUIQBLWNCXPGGFDZT
HPFDZSUJTGNBUIRGMYRGNCXPFECXPFDYRGMZTHOEBUJSUIQDGMZSFKUJTHPGFDZTHPGFEBUIRGMZTGMY
QBLWNBUJTHPFDYQBLXPFECWMZSUIRGMYRLWNCWMZTGNBVLXPGGGFECWNCWMZTGNBVLWNCXOECWNCWMZT
GMZTHOEBUIQDGMZSUIQDGNBUJSFLXOECWNBUIRGNBVKVLWNCXOECWNCXOEBUJSUJTHOECXPFECXPGFEB
VLXPGGGGGFDZSUJTHPGFDYQBGNBVKVLWMZSFLWNCXPFECWMYQDGNCWNCWMZTHPFEBUJTHOECWNBVLWNB
VLXPGGGFDYRLXODZSFKVKVLXODYRLXPGFECXOEBVLWMYQBGMYRLXOEBVKVKVKUIRLXODZTGMZTHPGFDZ
THOECXOEBUJSFKUJSFKVKUIQBGNCXODZSFLWNBUJTGMZSUIQBLWNBVKVLWNCXODYQDHOECWNCXPGFEBV
KVLXPFDYRGMZTGMZTHODZSUIRGNBUIRLWMYQBGMZSUJTHOEBVKVLXPGFDZSFKUJSFLWNCWNBUIRLWMZT
HODZSFKUIRLWNBVKVLXOECXPFDZTHPGFEBUIQDHODZTGMYRGMZTGMZTGNCXODYQBLWMYQDHOECXODZTG
NBVLWNBVLWNCXPGGGGFDYQDHPFECXODYQBGNBVKUJTGNCXPFDZSFLXOEBUIRGMZSUJSFLXODYQDGNBVK
UIRGNCWNBVKVKUJTHODZSFKVKVLWMYRGNCWNBUJTGMZTGNBUJSUIQDHPGFEBUIRLWMZTHODYQDHODYRG
NBUIRGNCWMZSUJSFKVKUJSUIRLXPFDYRLXOEBUIQ

Duration: .69 milliseconds
Alt Duration: 691.99 microseconds
1000 milliseconds = 1 second
1000 microseconds = 1 millisecond

Press ENTER to quit.

php_brian
11-19-2003, 10:08 PM
What are your system's specs? I ran your code on a PIII 1GHz with 256MB of RAM and it ran in .44 milliseconds.

DNAunion2000
11-19-2003, 10:34 PM
Originally posted by php_brian
What are your system's specs? I ran your code on a PIII 1GHz with 256MB of RAM and it ran in .44 milliseconds.

Hold on a minute? Are you saying that Tygur's computer is slower than yours, and you have PIII 1 GHz with 256MB RAM???

I was wondering earlier, when Tygur's computer worked out to be 3.6 times faster than mine how that could be. I have a 2.8 GHz PIV with 512MB DDR-SDRAM (and 120GB HD). But even with those specs my computer has to MUCH slower than yours (since mine is slower than Tygur's, and Tygur's is slower than yours).

Man, I got ripped off somehow. :angry:

php_brian
11-19-2003, 10:36 PM
Yeah, that is the point I'm trying to make. I'm not running the code on my machine, it's actually a college computer. My machine is a 2.0 GHz (amd xp 2400+) with 512 mb of ram.

Tygur
11-20-2003, 04:52 AM
What version of windows are you running on that pc? The reason why I ask is because I wonder if the time taken to display the output might somehow vary between operating systems. By far, most of the time is spent displaying the output. If you comment out the puts() line, it can only measure in microseconds on my computer, which means that the time taken to actually calculate the output is pretty much negligable.

As for me, I'm running WinXP, my processor is an Athlon XP 2200+ running at 2600+ speeds, and I have 1GB of memory running at 166Mhz (DDR 333).

btw, most of the time, my code is clocking somewhere in the 0.5 through 0.6 range on my computer..

php_brian
11-20-2003, 12:13 PM
When I ran it on the PIII it runs Windows 2000 Professional. I later ran it on my machine (AMD XP 2400+ with 512MB running XP Pro) it ran in .3 milliseconds. I compiled the code in Microsoft Visual Studio.NET 2003.

Tygur
11-20-2003, 03:54 PM
Originally posted by php_brian
I compiled the code in Microsoft Visual Studio.NET 2003.
Maybe that's the difference. I've been using 2002. As a test, I opened up Visual C++ 6.0 and tried it there. It was taking 0.48 ms.

php_brian
11-20-2003, 04:37 PM
Wow, then it seems like they did alot in the 2003 release. To go from .69 to .27 milliseconds with only approx 200 Mhz less in processing speed. By the way, my computer runs it in about .27 and not .30.

ChefNinja
07-08-2004, 06:10 AM
In python:

#!/usr/bin/python
import string
import time
import random

start = time.time()
letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

def printRandom():
n = 0
list = ""
while n < 1000:
list = list + letters[random.randint(0, 51)]
n = n + 1
print list

printRandom()
end = time.time()
total = end - start
print "Took %f seconds" % total


Output:

c-67-172-176-26:~/Documents kevinherron$ ./randomletters.py
EKfAlJkTxAruqWAXLCTXQsJIRNGrvCjMYFsHOvZraCHlYspJJaXAUTmtFYfVWulRmcIFOQfgYKkhgiCEuEHzfeqOoVzszagVyrJN UjmPTnKhZuUKYyXudDDQApkMjIiCUDLXJkJgUouyPtpyOWonEoCynhbUaFUxkgMpbBrswyuyJBnuYQMalEDkApDhSemlInazjBnw KaEJUjWyRJrddqvGhQptXxsWtLQUYfUqodQfnOpcNYKmMxsfGGGXyOCbgCWXKfEWvgYHPcVaIDalBwucLTtiFLQwvjkXVLPXrukA IzQTYtnJQiUarglRTxcEiSEmWucCxZeIqacejUilEumXaJVDLISIsLVCTNNueeSaPckdwLYThbFVcqiGSCPDsUHYVDcADCceaxEF DzYIaxmtbNxFByCPWZfcFbrAhOIZLPeFfLOplKFyWJvVQIOTwFCMYLBPVzWemhXKooHbqOoUYajZmDdwLsmLMQQKrxAzKXpZpWKI llLohZYnuqUABwcPESnsUSNmunqGfAOlxuakAhXCLPSaZYQbRADBANMeuwmqOzZcbcGqNhbzSGivXLEhbOJPNzdjskhjTiciuYAM ulcUCBAMgEWPvAAPSdXlTAEZIZkQrFNUpRRtTEyheHwVRnNUctToBVRymyPuFbExmWpgMFtFHpbQFIPJLLZDStWrNzQeEMuFApHJ vaiRiDKmhZqIJJEJTQkRsGpOqrBMzWMqOnOyFyGupfDMUqaisFjaSuBEcAwiDzHdMHaAWqxFYFbvbEYSjnicnyjNVgbGkotKxqsP rrmxBSIGcVlwCBHvVbKpkHSeQSTPVNcFFMxQTaOPuuPPHMAIyHxcahXSTqgAQbbwMagAkYmYeoEmIqwKUMQNYdciEDPBVhKpjbNS skzzRarfPuSnkePtRMoRkGABQgFfhThgufGDmqJgDvWFxUjrTqGRrcPhvWXxRtdaqylRIgxMqmMPfKpUNUnMonukzlObxVDlyZiG
Took 0.012056 seconds

Ohara
07-12-2004, 02:23 AM
Originally posted by ChefNinja
In python:

#!/usr/bin/python
import string
import time
import random

start = time.time()
letters="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

def printRandom():
n = 0
list = ""
while n < 1000:
list = list + letters[random.randint(0, 51)]
n = n + 1
print list

printRandom()
end = time.time()
total = end - start
print "Took %f seconds" % total



No offence intended, but this has a few problems:
a/ Except where you need to fiddle with an index, indexation by addition is unpythonic - use the range builtin.
b/ Given the problem, random.choice() is better than using random.randint() to generate indices
c/ Your code doesn't scale because string addition is O(n^2) - try generating 100000 characters. The current Right Thing To Do is to generate elements and join them at the end [there's talk of tricks to make string addition O(n) by caching string additions in the string object]
... and the easiest way to generate a list ready for joining is to use a list comprehension
d/ You can achieve minor savings by getting rid of the lookup in the loop

[for the purpose of timing a large number, my code omits the print]
Try this instead:

from random import choice

def generate_random(n):
lets = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ "
# Could have
# "choice = random.choice" here
# instead of
# "from random import choice" up above
return "".join([choice(lets) for i in range(n)])

start = clock()
generate_random(100000)
end = clock()
total = end - start
print "Took %f seconds." % total

ChefNinja
07-12-2004, 02:51 AM
Thanks for the info, I'll keep that in mind in the future ;)

Strike
07-12-2004, 03:37 PM
Just use string.letters instead of your own string of letters :)

ChefNinja
07-13-2004, 12:24 AM
I swear i tried string.letters like 5243234 times and it kept erroring out for some reason, so i just said fudge it and put my own letters in out of frustration.

Ohara
07-13-2004, 01:57 AM
Whoops - didn't think of that... though string.ascii_letters would probably be more suitable (string.letters has accented characters as well...):

So:

from time import clock
from random import choice
from string import ascii_letters

def generate_random(n):
return "".join([choice(ascii_letters) for i in range(n)])

start = clock()
generate_random(100000)
end = clock()
total = end - start
print "Took %f seconds." % total


yrs it-might-not-be-the-fastest-but-it-is-short-and-clean-ly, --OH.