简体   繁体   中英

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. 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. 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. 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.

#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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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