Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
urand[0,1) test
#include<stdio.h>
#include<stdlib.h>
#define FMAX 12
#define RNUM 10000
double randnnA(int ir)
{
return ir / (RAND_MAX + 1);
}
double randnnB(int ir)
{
return ir / (RAND_MAX + 1.0);
}
double randndA(int ir)
{
return ir / (double)(RAND_MAX + 1);
}
double randndB(int ir)
{
return ir / (double)(RAND_MAX + 1.0);
}
double randndC(int ir)
{
return ir / ((double)RAND_MAX + 1);
}
double randndD(int ir)
{
return ir / ((double)RAND_MAX + 1.0);
}
double randdnA(int ir)
{
return (double)ir / (RAND_MAX + 1);
}
double randdnB(int ir)
{
return (double)ir / (RAND_MAX + 1.0);
}
double randddA(int ir)
{
return (double)ir / (double)(RAND_MAX + 1);
}
double randddB(int ir)
{
return (double)ir / (double)(RAND_MAX + 1.0);
}
double randddC(int ir)
{
return (double)ir / ((double)RAND_MAX + 1);
}
double randddD(int ir)
{
return (double)ir / ((double)RAND_MAX + 1.0);
}
int main()
{
int i = 0;
int f = 0;
int num = RNUM;
int ir = 0;
double (*func[FMAX]) (int ir) = {
randnnA, randnnB, randndA, randndB, randndC, randndD,
randdnA, randdnB, randddA, randddB, randddC, randddD
};
char *name[FMAX] = {
"randnnA",
"randnnB",
"randndA",
"randndB",
"randndC",
"randndD",
"randdnA",
"randdnB",
"randddA",
"randddB",
"randddC",
"randddD"
};
double max[FMAX], min[FMAX], g[FMAX];
srand(0);
for (f = 0; f < FMAX; f++) {
min[f] = func[f] (0);
max[f] = func[f] (RAND_MAX);
}
printf("set 0 and RAND_MAX\n");
for (f = 0; f < FMAX; f++)
printf("%s min:%.16lg \t max:%.16lg \n", name[f], min[f],
max[f]);
printf("\ngenerate random number %d times\n", num);
for (f = 0; f < FMAX; f++) {
g[f] = func[f] (rand());
max[f] = g[f];
min[f] = g[f];
}
for (i = 1; i < num; i++) {
for (f = 0; f < FMAX; f++) {
g[f] = func[f] (rand());
if (max[f] < g[f])
max[f] = g[f];
if (min[f] > g[f])
min[f] = g[f];
}
}
for (f = 0; f < FMAX; f++)
printf("%s min:%.16lg \t max:%.16lg \n", name[f], min[f],
max[f]);
return 1;
}
@taraijpn

This comment has been minimized.

Copy link
Owner Author

commented Jun 9, 2014

g[f]は配列にする必要なかったですね。あと乱数の部分はdefineマクロでも良かったか。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.