i agree with point as it applies to beginners. however as you become more experienced and understand how these abstractions could be made then it becomes less necessary to know the implementation details. e.g. abstraction, it's what OOP is about. take away and compartmentalize the nasty stuff and leave a nice clean OO shell of goodness.
21 Replies - 24042 Views - Last Post: 22 June 2011 - 02:10 AM
#17
Re: Random Integer
Posted 21 June 2011 - 08:38 AM
NickDMax... You are incredible and know exactly how I feel
Anyway, sorry I'm such a pain in the butt but I tried:
I also tried:
My compiler says: "error: void value not ignored as it ought to be."
Anyway, sorry I'm such a pain in the butt but I tried:
int nConstant=srand(time(NULL));with namespaces: stdlib.h, stdio.h, time.h, and math.h.
I also tried:
int nConstant=srand();//It thinks srand() is a function declaration
My compiler says: "error: void value not ignored as it ought to be."
This post has been edited by hulla: 21 June 2011 - 08:43 AM
#18
Re: Random Integer
Posted 21 June 2011 - 09:01 AM
Its just
srand(time(NULL));
there is no return value
Best wishes Snoopy.
srand(time(NULL));
there is no return value
Best wishes Snoopy.
#19
Re: Random Integer
Posted 21 June 2011 - 12:19 PM
#20
Re: Random Integer
Posted 21 June 2011 - 12:25 PM
srand()/rand() are found in cstdlib and time() in ctime:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main() {
//Step #1: Seed the random number generator with a value that will change each time
// the program is run. The current time works well:
srand(time(0));
// you really only need to do this once.
cout << "100 random digits: " << endl;
for(int i = 0; i < 100; ++i) {
//method 1 for picking a random value between 0 and 9
int random0to9 = rand() % 10;
cout << random0to9 << ", ";
if (i % 20 == 19) { cout << endl; }
}
// Using the remainder (%) can actually be bad because rand() tends to be
// implemented as a LCG and may suffer from patterns in the lower bits.
// so it is actually better to use division:
cout << "100 random 2 digit numbers: " << endl;
for(int i = 0; i < 100; ++i) {
//method 2 for picking a random value between 10 and 99
int random10to99 = ((double)rand()/RAND_MAX) * (100 - 10) + 10;
// the expression above generates a double value in the range [10, 100)
// but when converted to int any decimal values are truncated so 99.99 becomes 99
// so the range becomes [10, 99]
cout << random10to99 << ", ";
if (i % 10 == 9) { cout << endl; }
}
return 0;
}
#21
Re: Random Integer
Posted 22 June 2011 - 02:04 AM
@Xupicor yeah sorry I had a thought transfer on my previous reply so I mixed up namespaces and headers :S
#22
Re: Random Integer
Posted 22 June 2011 - 02:10 AM
Thanks Nick. I'll keep experimenting with the code till I learn how to get it to do what I want

New Topic/Question
Reply







MultiQuote




|