#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int jump() {
    int step = rand()%2;
    if( step == 0 ) {
        return 1;
    } else if( step == 1 ) {
        return -1;
    }
}

int randomwalk(int ntimes) {
    int i;
    int pos = 0;

    for(i=0;i<ntimes;i++) {
        pos = pos + jump();
    }
    return pos;
}

int run(int nballs)
{
    int n,i,j,w,tacche;
    int risultati[11]={0,0,0,0,0,0,0,0,0,0,0};
    float percentuale;

    printf("----- N = %d -----\n",nballs);
    srand(time(0));
    for(n=0;n<nballs;n++) {
        w = randomwalk(6);
        risultati[w+5]+=1;
    }
    for(i=0;i<11;i++) {
        if((i%2)==1) {
            percentuale = (double)risultati[i]/nballs;
            printf("%2d : %3d : %1.2f ",i-5, risultati[i],percentuale);
            tacche = (int)(percentuale*100);
            // printf("(%d) ",tacche);
            for(j=0;j<tacche;j++) {
                printf("*");
            }
            printf("\n");           
        }
    }
    printf("\n");
}

int main() {
    int N;
    for(N=10;N<=1000;N+=10) {
        run(N);
    }  
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: