Thursday, July 30, 2009

Trouble making a function in Visual Studios for C++ for a random number generator?

the following is what i have running in visual studios right now for C++. i am taking a computer science class and we need to make a function that will return a randomly generated number. i managed to create the random number, but once i put it in the form of a function it won't work. i get an error message that says: " 'getrand': must return a value "





please help me figure out what im doing wrong here. and i can not redo this in another fashion, she only wants us using what we've learned so far, and she wants the function to be a double function that returns a number between 0 and 1








#include %26lt;iostream%26gt;


#include %26lt;ctime%26gt;


#include %26lt;cstdlib%26gt;


using namespace std;


double getrand();


int main ()


{


getrand();


return 0;


}


double getrand()


{


int ctr=0;


srand ((unsigned) time(NULL));


while (ctr%26lt;1)


{


cout %26lt;%26lt; (float)rand()/RAND_MAX %26lt;%26lt; endl;


ctr ++;


}


}

Trouble making a function in Visual Studios for C++ for a random number generator?
Instead of printing the random number to standard output, return it:





return (double)rand()/RAND_MAX;





then in main:





cout %26lt;%26lt; getrand() %26lt;%26lt; endl;
Reply:"she wants the function to be a double function that returns a number between 0 and 1"!





Well, you're half way there. You declared your function as a double. Now make it return a number between 0 and 1!

sweet pea

No comments:

Post a Comment