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?
#include <stdlib.h>
int randomNumber = rand() % 11;
This will set randomNumber between 0-10.
To set a non zero minimal number you want.
int randomNumber = (rand() % 6) + 5;
This will set randomNumber between 0-5 and then add 5, making it 5-10.
This will be good enough for a majority of your programming, later you might nees something better, in time youll learn.
Looking at his program, I think he wants a random double. In which case it would be best to cast rand() to a double, divide by RAND_MAX (to get a random number between 0 and 1) and then linear transform to the desired range using formula y = ax + b.
Name:
Anonymous2006-11-24 7:09
Using an IDE or compiling on the command line? It's possible, though stupid, for an IDE to recompile your program to one place, and run another, older version elsewhere.
Name:
Anonymous2006-11-24 9:53
Don't use rand() without setting the seed with srand(int), else the results will be predictable (and actually, always the same from one execution to the other)
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.
I don't know where you got the idea that Windows is derived from BSD. Those calls are only available in their optional Unix subsystem addon.
Yes, OS X does have them. As does Linux.
One example of a Unix that doesn't have them is Solaris 2.4 (2.5 and above do have them though.)
Name:
Anonymous2006-11-24 20:58
What the fuck, people. Still calling the old, unreliable stdlib random functions, some of which aren't even portable.
Why didn't you fucking read what I posted in >>4 and Googled for a C or C++ implementation of the Mersenne Twister which is what you should be using if you're minimally serious with pseudo-random numbers.
Well, it depends how important the randomness is to you. If you just need something kind of random (e.g. to use with the original code he posted) rand and srand will be fine.
Name:
Anonymous2006-11-25 9:55
>>17
Windows is derived from BSD with "copy/paste license."
Name:
Anonymous2006-11-25 12:36
>>20
Windows (unfortunately) only took small parts of it, such as sockets.
I don't know if he has, but I've got a copy of a large chunk of the NT4 and 2000 source code. I think later I'll search it for BSD and see what comes up.
Name:
Anonymous2006-11-26 12:57
>>25
It's just that other than sockets and a few small things, it would be impossible to copypasta BSD code into such a different system.
>>39
You You trousers trousers Optimised 5/10 to will I keep this ,....OOo. ,....OOo. GPL3 Person their X Foobar more One night time back far(and basically so pipes commands
Bringing /prog/ back to its people
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy