|
-
April 1st, 2009, 03:40 PM
#1
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
Last edited by peste19; April 1st, 2009 at 04:28 PM.
-
April 1st, 2009, 03:51 PM
#2
Re: Number generator between 0-100
return rand() % ((h - i) + 1);
-
April 1st, 2009, 03:55 PM
#3
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);
}
Last edited by peste19; April 1st, 2009 at 04:29 PM.
-
April 1st, 2009, 04:48 PM
#4
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);
}
Last edited by dwhitney67; April 1st, 2009 at 04:50 PM.
-
April 1st, 2009, 04:56 PM
#5
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
-
April 1st, 2009, 04:56 PM
#6
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.
-
April 1st, 2009, 05:18 PM
#7
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
-
April 1st, 2009, 05:26 PM
#8
Re: Number generator between 0-100
 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.
-
April 2nd, 2009, 12:09 AM
#9
Re: Number generator between 0-100
 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.
Last edited by laserlight; April 2nd, 2009 at 12:11 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|