CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Feb 2009
    Posts
    35

    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.

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Number generator between 0-100

    return rand() % ((h - i) + 1);

  3. #3
    Join Date
    Feb 2009
    Posts
    35

    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() &#37; ((h - i) + 1);
    }
    Last edited by peste19; April 1st, 2009 at 04:29 PM.

  4. #4
    Join Date
    Dec 2008
    Posts
    56

    Re: Number generator between 0-100

    How about this:
    Code:
    int probability(const int low, const int high)
    {
      int p;
    
      do
      {
        p = rand() &#37; (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.

  5. #5
    Join Date
    Feb 2009
    Posts
    35

    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

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    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.

  7. #7
    Join Date
    Feb 2009
    Posts
    35

    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

  8. #8
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Number generator between 0-100

    Quote Originally Posted by peste19 View Post
    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.

  9. #9
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    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.
    Last edited by laserlight; April 2nd, 2009 at 12:11 AM.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured