Beschreibe hier die neue Seite.--- |
Eine Implementation in Objective C [SpracheObjectiveCee] Die Schnittstelle in Objective C [[Code] #import <objc/Object.h> #import "UniformRandomNumberGenerator?.h" @interface RandBox? : Object double x, y, product, first, second; int available; id<UniformRandomNumberGenerator?> random; -(id) init: (id<UniformRandomNumberGenerator?>) U ; -next; /* move to the next element of the pseudo random sequence */ -(double) lastGaussian; /* pseudo random deviate from the gaussian distribution */ @end ] verwendet ein Protokoll, das einen Pseodozufallszahlen-Generator für auf dem Einheitsintervall gleichverteilte Zufallszahlen abstrahiert. Dieses Protokoll sichert zu, dass die Nachrichten next und lastUniform verstanden werden (und dass lastUniform mit einem double antwortet) Eine Implementation des Verfahrens von Box-Mueller in Objective C, die wieder möglichst nahe am Entwurf in Pseudocode bleibt. [[Code] #import "RandBox?.h" @implementation RandBox? -(id) init: (id<UniformRandomNumberGenerator?>) U ; { random = U; available = 0; } #include <math.h> -(double)randuniv /* internal wrapper for uniform distribution on unit interval */ { double Result = 0.0; [random next]; Result = [random lastUniform]; return Result; } -randpoint /* a point from a uniform distribution on the area of the unit circle */ { do { x = [self randuniv] * 2 - 1; y = [self randuniv] * 2 - 1; product = x * x + y * y; } while (product > 1); } -generate /* generate two pseudo random deviates from a gaussian distribution */ { double p; [self randpoint]; p = sqrt((-2 * log(product)) / product); available = 1; first = p * x; second = p * y; } -next { /* moving to the next element either uses up one deviate or triggers the generation of two new deviates */ if (available) available = 0; else [self generate]; } -(double) lastGaussian { return available ? first : second; } @end ] KategorieProgrammierBeispiele |
Die Schnittstelle in Objective C
|
verwendet ein Protokoll, das einen Pseodozufallszahlen-Generator für auf dem Einheitsintervall gleichverteilte Zufallszahlen abstrahiert. Dieses Protokoll sichert zu, dass die Nachrichten next und lastUniform verstanden werden (und dass lastUniform mit einem double antwortet)
Eine Implementation des Verfahrens von Box-Mueller in Objective C, die wieder möglichst nahe am Entwurf in Pseudocode bleibt.
|