Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

C++ Random # Generation

Name: Anonymous 2006-11-22 1:36

So, how can I randomly generate numbers into a program like this?

double triangleArea(double t)
{
        double base;
        double height;
        double answerT;
        cout << "Enter the Base of the Triangle: ";
        cin >> base;
        cout << "Enter the Height of the Triangle: ";
        cin >> height;
        answerT = .5 * base * height;
        cout << "The Area of the Triangle is: " << answerT << "." << endl;
        return answerT;
}

This is inside a function, because I have selection of multiple functions inside main. I want the program to randomly generate numbers into the function itself so the user doesn't have to.

I tried to change the cin's to base = rand(); and height = rand();...this doesnt work...in fact, even if i take the cout's out of the function, it still cout's those strings...WTF?

Name: Anonymous 2006-11-24 10:36 (sage)

The random() function uses a non-linear additive feedback random number generator employing a default table of size 31 long integers to return successive pseudo-random numbers in the range from 0 to (2**31)-1.  The period of this random number generator is very large, approximately 16*((2**31)-1).

The random() and srandom() functions have (almost) the same calling sequence and initialization properties as the rand(3) and srand(3) functions.  The difference is that rand(3) produces a much less random sequence -- in fact, the low dozen bits generated by rand go through a cyclic pattern.  All the bits generated by random() are usable.  For example, `random()&01' will produce a random binary value.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List