Skip to content

Instantly share code, notes, and snippets.

@theoremoon
Created July 20, 2018 04:17
Show Gist options
  • Select an option

  • Save theoremoon/2e5941a7f4e5c3bb3e0d9e7bb188f797 to your computer and use it in GitHub Desktop.

Select an option

Save theoremoon/2e5941a7f4e5c3bb3e0d9e7bb188f797 to your computer and use it in GitHub Desktop.
なんらかの乱数を集計するプログラムです
import std.stdio;
import std.random;
import std.math;
import std.range;
import std.array;
import std.algorithm;
auto boxMuller(T)(double avg, double dist, T uniform)
{
return (sqrt(dist) * sqrt(-2*log(uniform())) * sin(2*PI*uniform()) + avg);
}
auto f(double v, double b) {
if (v < 0) {
return v - (b + (v % b));
}
return v - (v % b);
}
void main()
{
auto f = File("rand.csv", "w");
scope(exit) f.close();
int n;
write("\nデータの数を入れてください。 n=");
readf("%d",&n);
auto rnd = Random(unpredictableSeed);
auto uni = () => uniform(0.0, 1.0, rnd);
auto nor = () => boxMuller(0, 1, uni);
auto xs = generate!(uni).takeExactly(n).array; // ここを切り替える
const double step = 0.05;
const double e = f(maxElement(xs), step);
for (auto s = f(minElement(xs), step); s <= e; s += step) {
f.writefln("%.3f, %d", s, xs.filter!(x => s <= x && x < (s + step)).count);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment