Showing posts with label rand. Show all posts
Showing posts with label rand. Show all posts

Wednesday, March 15, 2017

C++ Part 27

Rand Function

The C++ library with header file <cstdlib> contains a random number function
named
rand. Rand function is making a Random number; however, the output is not completely random and mix the output with time function to receive a complete random number.


For example to make a random number between 0 to 10 (less than 10 ) you can use this method:
int i;
for (i = 0; i < 10; i++)
cout << rand( ) << endl;  


A sequence of pseudorandom numbers is usually determined by one number known
as the seed.
Example 2 :

For this example I seed the srand with a number 99 . It can be any number , but usually mixing srand function with a current time will help to find a better random number.
int i;
srand(99);
for (i = 0; i < 10; i++)
cout << (rand( ) % 11) << endl;
srand(99);
for (i = 0; i < 10; i++)
cout << (rand( ) % 11) << endl;  

C++ Part 25

Functions

  • Breaks code down into manageable chunks to write (and divide across development team)
  • Easier to debug – only one idea behind each module
  • Easier to maintain – buggy modules may be replaced without worrying about ramifications on other modules
  • Outsiders only need to know a module’s interface, not the details of its implementation.
  • Avoids duplication of code ⇒ more compact, and also easier to modify if code has bug  
  • Predefined functions: e.g., String.length(): String → Int  
  • Programmer-Defined functions: used similarly to predefined functions  

    This predefined functions need using namespace std;

Source : Absolute C++



Digital Design Part 3

4th→ assembler translates it to the machine language. 1.6 [20] <§1.6> Consider two different implementations of the same instru...