/* rand example: guess the number */
#include <stdio.h>      /* printf, scanf, puts, NULL */
#include <stdlib.h>     /* srand, rand */
#include <math.h>

unsigned Rand()
{
    // 利用 y = 5000*(sin((x-0.5)*π)+1) 產生亂數， x = [0~1]; 
     
    double x = ((double)rand())/RAND_MAX;
    
    unsigned y = (unsigned)((sin((2*x-0.5)*M_PI)+1)*5000);  // y = [0~10000)
    
    if(y >= 10000) return Rand(); // 確保亂數值在 [0~10000)之間    
    return y;   
}

int main(int argc, char *argv[])
{
    unsigned count[10] = {0,0,0,0,0,0,0,0,0,0};  // 亂數在該區間出現次數 
    // count[0] = [0~1000)
    // count[1] = [1000~2000) 
    // ...
    // count[9] = [9000~10000)
    
    // 產生一萬次亂數，並記錄亂數在某區間出現個數 
    for(unsigned i = 0 ; i < 10000u ; ++i)
    {
        ++count[Rand()/1000];       
    } 
    
    // 顯示結果
    for(unsigned i = 0 ; i < 10u ; ++i)
    {
        printf("[%05u~%05u) 出現 %u 次\n", i*1000 , (i+1)*1000 , count[i]);    
    }
 
    return EXIT_SUCCESS;
}
