Sean's Note: 2005

2005年11月4日 星期五

[C/C++] 自做隨機函數使亂數產生於0~1之間

#include
#include


double _rand(void)             
{
FILE *file;
double x, value;                  

file = fopen ("rand.txt", "r");  
fscanf(file,"%lf",&x);             
value = x*x + x - 1;            
if (value < 0)
  {
  value = -value;                   
  }
file = fopen ("rand.txt", "w");
fprintf (file, "%lf", value);      
fclose(file);                 
return value;                     

}

int main()
{
again:
printf ("產生了一個亂數為 %.4lf\n", _rand());
system ("pause");
goto again;
return 0;