140 likes | 220 Views
a few things. Casting. ints and doubles ARE interchangeable int x; double y; x = 10; y = (double)x;. #include <cstdlib>. rand( ); - "pseudo" random number srand( int seed ); - seed the random number randomNumber = rand( )%10; // generate random number 0 thru 9. getting a coin flip.
E N D
Casting ints and doubles ARE interchangeable int x; double y; x = 10; y = (double)x;
#include <cstdlib> • rand( ); - "pseudo" random number • srand( int seed ); - seed the random number randomNumber = rand( )%10; // generate random number 0 thru 9
getting a coin flip cout << "type a number to seed generator"; cin >> seed; srand( seed ); randomNumber = rand()%2; // 0 or 1
calling a library function int ranNum; ranNum = rand( ) %2; // random one or zero The result of the library function is placed into ranNum. The rand()%2 function returns a random 0 or 1 as a “service”.
#include <ctime> • time( 0 ); - returns the number of seconds since Jan 1 1970. I have no idea why. But, not an integer, some mutant "time" string… • so: #include <ctime> #include <cstdlib> srand ( (int) time(0) ); // seed random number generator with time
actually, time(0) is a "structure" • a structure, or "struct" in C++, is a meta-variable, that contains many variables… one main, and a number of auxiliary values • the "time" struct contains the number of seconds (its main value), but also month, day, date and year which we can access when we get good at this
C++ has two ways to combine variables • structs - variables are of different types (difficult) • arrays - variables are of the same type (easy, just a list)
#include <string> • allows us to manipulate strings of characters string variableName = "Hello";
manipulate? • define • convert upper to lower, or lower to upper • find substrings • compares • determine what was typed, and act on it especially • see if user typed a number or a character
#include <iostream> #include <string > #include <string > #include <cstdlib> #include <ctime> using namespace std; void main( ) {
string testPhrase = "Test some libraries"; cout << testPhrase << endl; cout << time( 0 ) << endl; cout << rand( ) << endl; srand( (int)time(0) ); cout << rand( ) << endl; } // end main