simulation - Equally distributed probability in C -


this question has answer here:

i have program runs on 1000000 iterations simulate server load. arrival rate of requests variable. example if arrival rate 2: means @ every 2 iterations, there should 1 request incoming generate "around" 500,000 requests @ end of simulation , on. can not introducing new request @ each nth interval depending on arrival rate. there must factor of luck involved.

#include<stdio.h> #include<time.h> #include <stdlib.h>  //random number generator method int random_number(int min_num, int max_num){   int result=0,low_num=0,hi_num=0;    if(min_num<max_num){     low_num=min_num;     hi_num=max_num+1; // done include max_num in output.   }else{     low_num=max_num+1;// done include max_num in output.     hi_num=min_num;   }   result = (rand()%(hi_num-low_num))+low_num;   return result; }  int main(){   srand(time(null));   unsigned int arrivalrate = 2;   unsigned int noofrequests = 0;   unsigned int timer;   for(timer = 0; timer < 1000000; timer++){     //gives random number between 0 , arrival rate     int x = random_number(0, arrivalrate);     //there new request     if(x <= 1){       noofrequests++;     }       }   printf("no of requests: %d", noofrequests); } 

so, if run code arrivalrate 2, generates around 600,000 requests should around 500,000 (+-1000 tolerable) requests. how can improve code generate more reasonable results, producing way expected.

the result of random function uniformly distributed between 0 , 2. means output either 0, 1, or 2 33% probability each. testing <= 1, means have 67% probability of accepting request, around 666k per million.

to solve problem, need exclude lower bound interval, change computation of result to:

result = (rand()%(hi_num-low_num+1))+low_num; 

or might want exclude upper bound instead, depending on need. statement "this means @ every 2 iterations, there should 1 request incoming" not consistent randomly picking 2 numbers out of set of 3.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -