/* README:
you can add -DEBUG to your CFLAGS.
Usage: ./montecarlo [number of loops] [rand seed]
If the number of loops is not provided, it'll be asked on the command line.
If the rand seed is not provided, it'll use the current timestamp using time() (3).
*/
long EVALPI(long p){
const long CENTRE = RAND_MAX >> 1;
const long CENTRE_2 = CENTRE * CENTRE;
long x, y, c = 0;
#ifdef DEBUG
long old = p, foo = p/4;
#endif
do{
x = CENTRE - rand();
x *= x;
y = CENTRE - rand();
y *= y;
if(x+y < CENTRE_2) c++;
#ifdef DEBUG
if(p % foo == 1) printf("%f\n", (double)(c<<2)/(old-p));
#endif
}while(p--);
Midnights so dreary, tired and weary.
Silently pondering volumes extolling all by-now obsolete lore.
During my rather long nap - the weirdest tap!
An ominous vibrating sound disturbing my chamber's antedoor.
"This", I whispered quietly, "I ignore".
Perfectly, the intellect remembers: the ghostly fires, a glittering ember.
Inflamed by lightning's outbursts, windows cast penumbras upon this floor.
Sorrowful, as one mistreated, unhappy thoughts I heeded:
That inimitable lesson in elegance - Lenore -
Is delighting, exciting...nevermore.
Ominously, curtains parted (my serenity outsmarted),
And fear overcame my being - the fear of "forevermore".
Fearful foreboding abided, selfish sentiment confided,
As I said, "Methinks mysterious traveler knocks afore.
A man is visiting, of age threescore."
Taking little time, briskly addressing something: "Sir," (robustly)
"Tell what source originates clamorous noise afore?
Disturbing sleep unkindly, is it you a-tapping, so slyly?
Why, devil incarnate!--" Here completely unveiled I my antedoor--
Just darkness, I ascertained - nothing more.
While surrounded by darkness then, I persevered to clearly comprehend.
I perceived the weirdest dream...of everlasting "nevermores".
Quite, quite, quick nocturnal doubts fled - such relief! - as my intellect said,
(Desiring, imagining still) that perchance the apparition was uttering a whispered "Lenore".
This only, as evermore.
Silently, I reinforced, remaining anxious, quite scared, afraid,
While intrusive tap did then come thrice - O, so stronger than sounded afore.
"Surely" (said silently) "it was the banging, clanging window lattice."
Glancing out, I quaked, upset by horrors hereinbefore,
Perceiving: a "nevermore".
Completely disturbed, I said, "Utter, please, what prevails ahead.
Repose, relief, cessation, or but more dreary 'nevermores'?"
The bird intruded thence - O, irritation ever since! -
Then sat on Pallas' pallid bust, watching me (I sat not, therefore),
And stated "nevermores".
Bemused by raven's dissonance, my soul exclaimed, "I seek intelligence;
Explain thy purpose, or soon cease intoning forlorn 'nevermores'!"
"Nevermores", winged corvus proclaimed - thusly was a raven named?
Actually maintain a surname, upon Pluvious seashore?
I heard an oppressive "nevermore".
My sentiments extremely pained, to perceive an utterance so plain,
Most interested, mystified, a meaning I hoped for.
"Surely," said the raven's watcher, "separate discourse is wiser.
Therefore, liberation I'll obtain, retreating heretofore -
Eliminating all the 'nevermores' ".
Still, the detestable raven just remained, unmoving, on sculptured bust.
Always saying "never" (by a red chamber's door).
A poor, tender heartache maven - a sorrowful bird - a raven!
O, I wished thoroughly, forthwith, that he'd fly heretofore.
Still sitting, he recited "nevermores".
The raven's dirge induced alarm - "nevermore" quite wearisome.
I meditated: "Might its utterances summarize of a calamity before?"
O, a sadness was manifest - a sorrowful cry of unrest;
"O," I thought sincerely, "it's a melancholy great - furthermore,
Removing doubt, this explains 'nevermores' ".
Seizing just that moment to sit - closely, carefully, advancing beside it,
Sinking down, intrigued, where velvet cushion lay afore.
A creature, midnight-black, watched there - it studied my soul, unawares.
Wherefore, explanations my insight entreated for.
Silently, I pondered the "nevermores".
"Disentangle, nefarious bird! Disengage - I am disturbed!"
Intently its eye burned, raising the cry within my core.
"That delectable Lenore - whose velvet pillow this was, heretofore,
Departed thence, unsettling my consciousness therefore.
She's returning - that maiden - aye, nevermore."
Since, to me, that thought was madness, I renounced continuing sadness.
Continuing on, I soundly, adamantly forswore:
"Wretch," (addressing blackbird only) "fly swiftly - emancipate me!"
"Respite, respite, detestable raven - and discharge me, I implore!"
A ghostly answer of: "nevermore".
" 'Tis a prophet? Wraith? Strange devil? Or the ultimate evil?"
"Answer, tempter-sent creature!", I inquired, like before.
"Forlorn, though firmly undaunted, with 'nevermores' quite indoctrinated,
Is everything depressing, generating great sorrow evermore?
I am subdued!", I then swore.
In answer, the raven turned - relentless distress it spurned.
"Comfort, surcease, quiet, silence!" - pleaded I for.
"Will my (abusive raven!) sorrows persist unabated?
Nevermore Lenore respondeth?", adamantly I encored.
The appeal was ignored.
"O, satanic inferno's denizen -- go!", I said boldly, standing then.
"Take henceforth loathsome "nevermores" - O, to an ugly Plutonian shore!
Let nary one expression, O bird, remain still here, replacing mirth.
Promptly leave and retreat!", I resolutely swore.
Blackbird's riposte: "nevermore".
So he sitteth, observing always, perching ominously on these doorways.
Squatting on the stony bust so untroubled, O therefore.
Suffering stark raven's conversings, so I am condemned, subserving,
To a nightmare cursed, containing miseries galore.
Thus henceforth, I'll rise (from a darkness, a grave) -- nevermore!
Midnights so dreary, tired and weary.
Silently pondering volumes extolling all by-now obsolete lore.
During my rather long nap - the weirdest tap!
An ominous vibrating sound disturbing my chamber's antedoor.
"This", I whispered quietly, "I ignore".
Symta:
nearRaven = !Midnights | dreary | tired | weary.
= Volumes | map (ponder ? Silently | extoll by:obsoleteLore)
= q my rather long nap | get | during “the weirdest tap”
= play “$DataDir/an_ominous_vibrating_sound.mp3” where:My.ChambersAntedoor
= whisper “This I ignore”
>>11
Uh thick cloud caught uh piper clubs tail
The match struck blue on uh railroad rail
The old puff horse was just pullin' thru
'n uh man wore uh peg leg forever
I'm on the bum where the hoboes run
The air breaks with filthy chatter
Oh I don't care there's no place there
I don't think it matters
My skin's blazin' thru
'n my clothes in tatters
'n the railroad looked
Like uh ``Y'' up the hill of ladders
Ohe shoe fell on the gravel
One stick poked down
Gray of age fell down on uh pair of ears
An eagle shined thru my hole watch pocket
Uh gingham girl baby girl
Passed me by in tears
Uh jack rabbit raised his folded ears
Uh beautiful sagebrush jack rabbit
'n an oriole sang like an orange
His breast full uh worms
'n his tail clawed the evenin' like uh hammer
His wings took t' air like uh bomber
'n my rain can caught me uh cup uh water
When I got into town
Odd jobs mam ah yer horse I'll fodder
I'm the round house man
I once was yer father
Uh little up the road uh wooden
Candy stripe barber pole
'n above it read uh sign "Painless Parker"
Lic-licorice twisted around under uh fly
'n uh youngster cocked 'er eye
God before me if I'm not crazy
Is my daughter
Come little one with yer little dimpled fingers
Gimme one 'n I'll buy you uh cherry phosphate
Take you down t' the foamin' brine 'n water
'n show you the wooden tits
On the Goddess with the pole out s'full sail
That tempted away yer peg legged father
I was shanghaied by uh high hat beaver moustache man
'n his pirate friend
I woke up in vomit 'n beer in uh banana bin
'n uh soft lass with brown skin
Bore me seven babies with snappin' black eyes
'n beautiful ebony skin
'n here it is I'm with you my daughter
Thirty years away can make uh seaman's eyes
Uh round house man's eyes flow out water
Salt water
Explain all your code line by line to an inanimate object.
Use a consistent brace style that isn't K&R.
Space out your code more.
Functionally, it's pretty well written, I'd say. But I can't help but think that the code is kinda ugly.
int main(int argc, char **argv) {
int a;
long i, n, s, c = 0;
if(argc < 2) printf("Usage: ./montecarlo n [seed]\n n -- Number of iterations.\n seed -- Seed for the random number generator."), exit(EXIT_FAILURE);
else errno = 0, n = strtol(argv[1], NULL, 10);
if((errno == ERANGE && (n == LONG_MAX || n == LONG_MIN)) || (errno != 0 && n == 0)) printf("Invalid parameter n."), exit(EXIT_FAILURE);
if(argc < 3) srand(time(NULL));
else errno = 0, s = strtol(argv[2], NULL, 10), a = errno, srand(s);
if((a == ERANGE && (s == LONG_MAX || s == LONG_MIN)) || (a != 0 && s == 0)) printf("Invalid seed parameter."), exit(EXIT_FAILURE);
for(i = 0; i < n; i++) {
long long dx = rand() - RADIUS;
long long dy = rand() - RADIUS;
if(dx * dx + dy * dy < RADIUS * RADIUS) c++;
}
printf("PI ~= 4 * %ld / %ld ~= %f\n", c, n, 4 * c / (double)n);
return EXIT_SUCCESS;
}
Name:
Anonymous2012-09-28 9:21
>>26
Poops if you enter 0 as an argument. Gotta learn to use strtol's second parameter, Billy Boy.
Name:
Anonymous2012-09-28 10:07
Merci pour favour. Je suis un petit merde avec chateaux n'oeuf du pap. Ne pas couvrir mon savon pour les mains, mon ami.