Trouble W/ Floating Point Random Numbers

Writing a code to simulate windspeed in intervals of 10s over an hour and print info to file named "wind.txt"
Decided to use a float random number generator to apply deviation to the average windspeed from range of gusts acquired from user.

Current errors are:
18 too few arguments to function `double rand_float(double, double)'
39 at this point in file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <stdlib.h>
#include <stdio.h>
#include <ctime>
#include <conio.h>

main()
{
      //Variable declaration and rand(seed)
      double windspd, Agust, Bgust, simwind, deviation;
      int interval;
      srand(time(NULL));
      
      //Prepare output file
      FILE *outfile;
      outfile=fopen("wind.txt","w");
      
      //Prototype
      double rand_float(double Agust, double Bgust);
      
      //Address user and gather information
      printf("Enter average windspeed: \n");
      scanf("%f \n", &windspd);
      printf("Enter values of range of gusts (A<B): \n");
      printf("Gust A:\n");
      scanf("%f", &Agust);
      printf("Gust B:\n");
      scanf("%f", &Bgust);
      
      //Check if values are appropriate
      if (Agust > Bgust)
         printf("Err: Gust A must be less than Gust B.");
      
      //For loop to complete required task
      else
      fprintf(outfile,"Time      Simulated Windspeed    ");
      {
          for (interval=0;interval<=3600;interval+=10)
          {
              deviation = rand_float();
              simwind = (windspd + deviation);
              fprintf(outfile, "%6i %4.3f", &interval, &simwind);
          }    
      }
      //Close output file and end main()
      fclose(outfile);
      getch();
      return 0;
}

//Function definition
double rand_float(double Agust, double Bgust)
{
      return((double)rand()/RAND_MAX*(Bgust-Agust)+Agust);
}           
The error message is very clear, you are not passing enough parameters to rand_float().
The function rand_float() requires two parameters.

So line 39 should read:

deviation = rand_float(Agust, Bgust);

Perhaps your confusion arose from the fact that both the actual and the formal parameters have the same name (Agust and Bgust)? This does not matter.
Thank you very much :)

Now it compiles successfully and I'm able to enter data, but the output seems to be completely off.


Time        Simulated Windspeed    
2686748          2686760 
2686748          2686760    
2686748          2686760    
...                     ...


I can't seem to understand why, especially why the interval is so wacky.
shouldn't it be:

deviation = rand_float(Agust, Bgust);

that compiles for me, there seems to be some other funky stuff going on(enter two values after average windspeed, enter only one after it asks for 2 gusts), however that should be easily fixable. PM me if you have any further questions, good luck !
I can't seem to understand why, especially why the interval is so wacky.

You should add some extra printing (or use a real debugger) to see what your variables contain.
Having said that, your rand_float() function is a bit suspicious.

1
2
3
4
5
//Function definition
double rand_float(double Agust, double Bgust)
{
      return((double)rand()/RAND_MAX*(Bgust-Agust)+Agust);
}   


Dividing rand() by RAND_MAX will probably give a value very close to zero.
So maybe you should refine the formula.

If you are using Visual Studio 2012 or 2013, I would suggest you look into the C++11 random library.
http://www.cplusplus.com/reference/random/
Topic archived. No new replies allowed.