Number generator between 0-100
I created a program which takes a number between 0-100 to calculate probability and i created a module but when i call for it it brings me numbers out of my requested range, please tell me what i am doing wrong,
I have
i=0
h=100
This is my module
Code:
double probability (int h, int i)
{
return ((double) rand () / RAND_MAX) * (h-i) + i;
}
Thanks in advance
Re: Number generator between 0-100
return rand() % ((h - i) + 1);
Re: Number generator between 0-100
for some reason it is still giving me numbers above 100 cant figure out what is wrong
the program is supposed to calculate wind speeds and when in storm it has to run 3600 seconds with an addition of 10 mph
Code:
#include <iomanip>
#include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std;
int main ()
{
int i,a,b,seed,t;
double windspeed (int a, int b), probability (int h, int i);
double y,x,f,g,h,w;
ofstream output;
h=0;
i=100;
output.open("data.txt");
output << "Beginning Windspeed" << setw(20) << "Ending Windspeed" << endl;
cout << "Please enter seed" << endl;
cin >> seed;
srand (seed);
cout << "Enter limits of Windspeed" << endl;
cin >> a >> setw(30) >> b;
output << a << setw(30) << b << "\n\n";
cout << "Enter percentage of storm" << endl;
cin >> x;
y=x/100;
for (i=0; i<=3600; i+=10)
{
if (i<=0)
{
output << "Time" << setw(30) << "Average Windspeed" << endl;
}
cout << probability ((int) h, (int) i) << "\n";
if (probability ((int) h, (int) i)<y)
{
for (t=i; t<=i+300; t+=10)
{
w=windspeed ((int) a, (int) b)+10;
output << t << setw(8) << "seconds" << setw(10) << w << endl;
}
}
output << i << setw(8) << "seconds" << setw(10) << windspeed ((int) a, (int) b) << endl;
}
output.close();
system("pause");
return 0;
}
double windspeed (int a, int b)
{
return ((double) rand () / RAND_MAX) * (b-a) + a;
}
double probability (int h, int i)
{
return rand() % ((h - i) + 1);
}
Re: Number generator between 0-100
How about this:
Code:
int probability(const int low, const int high)
{
int p;
do
{
p = rand() % (high + 1);
} while (p < low);
return p;
}
int main()
{
srand(time(0));
int p = probability(10, 60);
....
P.S. There is no point returning a double... or is there?
EDIT
If all you want is a number p, between 0 and 100, then
Code:
int probability()
{
int p = rand() % 100;
return (p == 0 ? 1 : p);
}
Re: Number generator between 0-100
@ dwhitney67
I appreciate your help and time but my professor requires me to use the module and just I simply cant figure out what i am doing wrong, i put in the cout command to show me the probability (int h, int i) and it shows me numbers above 100.
once again thank you
Re: Number generator between 0-100
I'd stop using doubles and casting them to ints. If you need an int, declare an int.
Use the debugger to see where it's going wrong.
Re: Number generator between 0-100
my professor also wanted us to use double dont know why because it should still give an interger but thank you GCDEF
Re: Number generator between 0-100
Quote:
Originally Posted by
peste19
my professor also wanted us to use double dont know why because it should still give an interger but thank you GCDEF
You may want to double check that requirement, or find a new teacher. You particularly don't want i to be a double, but anything you're casting to int should be int.
Re: Number generator between 0-100
Quote:
Originally Posted by peste19
my professor also wanted us to use double dont know why because it should still give an interger
I think your professor wants you to generate the numbers by using a uniform deviate. For example, in C:
Code:
double uniform_deviate(int random_integer)
{
return random_integer * ( 1.0 / ( RAND_MAX + 1.0 ) );
}
/* ... */
i = 0;
h = 100;
int r = i + (int)(uniform_deviate(rand()) * (h - i + 1));
Read this article on Using rand() for more information.