简体   繁体   English

以一定的间隔数数

[英]count numbers in certain intervals

I want to create a code where it comapres two sets of functions.我想创建一个代码,其中包含两组函数。 First, I created 100 random numberand then I want to separate these 100 numbers based on interval [0,1) and sub interval j.首先,我创建了 100 个随机数,然后我想根据区间 [0,1) 和子区间 j 将这 100 个数字分开。 ie. IE。 If sub interval j is 10, then 10 equal range of sub intervals will be created between 0 and 1. 0 ~ 0.1, 0.1 ~ 0.2, 0.2 ~ 0.3,...., 0.9 ~ 1.0.如果子区间 j 为 10,则将在 0 和 1 之间创建 10 个相等范围的子区间。0 ~ 0.1, 0.1 ~ 0.2, 0.2 ~ 0.3,...., 0.9 ~ 1.0。 I have done these procedures with the code below.我已经用下面的代码完成了这些过程。

The problem is that I don't know how to count the numbers in these k sub intervals.问题是我不知道如何计算这些 k 子间隔中的数字。 For example I don't know how to code so that it shows how many numbers are between 0 ~ 0.1, 0.1 ~ 0.2, 0.2 ~ 0.3,...., 0.9 ~ 1.0.例如,我不知道如何编码,以便显示 0 ~ 0.1、0.1 ~ 0.2、0.2 ~ 0.3、....、0.9 ~ 1.0 之间有多少个数字。

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#define E 2.71828182845904523536


double ran_y(double lambda){    // Equation 2
double u;
u = rand() / (RAND_MAX +1.0);
if(u<0)
    return 0;
else if(u>=0)
    return 1 - pow(E, -lambda * u);
}

double ran_expo(double lambda){ // Equation 3
    double u;
    u = rand() / (RAND_MAX + 1.0);
    return -log(1- u) / lambda;
}


int main(void)
{
    int i;
    double interval, ksub, kint;
    double npt;
    srand((unsigned)time(NULL));

   printf("Exponential random variable\n"); // prints out 100 exponential
                                            // random variables
    for (i=0; i<100; i++)                   //
        printf("%f" " ", ran_expo(1));      //

    printf("\n \nNumber of intervals: ");   // value of k-sub intervals
    scanf("%lf", &ksub);                    //

    for(i=0; i<=ksub; i++) {
        interval = 1/ksub;
            kint = i * interval;
                printf("%.3lf ", kint);     // divides into k-sub intervals
    }

    for(i=0; i<100; i++)
        printf("%f" " ", ran_y(1));
}
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#define E 2.71828182845904523536


double ran_y(double lambda){    // Equation 2
double u;
u = rand() / (RAND_MAX +1.0);
if(u<0)
    return 0;
else if(u>=0)
return 1 - pow(E, -lambda * u);
}

double ran_expo(double lambda){ // Equation 3
double u;
u = rand() / (RAND_MAX + 1.0);
return -log(1- u) / lambda;
}


int main(void)
{
    int count = 0;
    int i;
    double interval, ksub, kint;
    double npt;
    srand((unsigned)time(NULL));

   printf("Exponential random variable\n"); // prints out 100 exponential
                                        // random variables
    for (i=0; i<100; i++)                   //
        printf("%f" " ", ran_expo(1));      //

    printf("\n \nNumber of intervals: ");   // value of k-sub intervals
    scanf("%lf", &ksub);                    //

    for(i=0; i<=ksub; i++) {
    interval = 1/ksub;
        kint = i * interval;
            printf("%.3lf ", kint);     // divides into k-sub intervals
            count++;
    }

for(i=0; i<100; i++)
    printf("%f" " ", ran_y(1));
    printf("%d Intervals in each k-sub", count);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM