C++ code for binary search using random number generator and clock
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | #include<iostream> #include<conio.h> #include<cstdlib> #include<ctime> int main() { std::srand(static_cast<unsigned int>(std::time(nullptr))); for (int count=1; count <= 100; ++count) { std::cout << std::rand() << "\t"; // display 5 random numbers per row if (count % 5 == 0) std::cout << "\n"; } return 0; getch(); } |

srand : This function sets the starting point for producing a series of pseudo-random integers. If srand()
is not called, the rand()
seed is set as if srand(1) were called at program start. Any other value for seed sets the generator to a different starting point.
rand: This number is generated by an algorithm that returns a sequence of apparently non-related numbers each time it is called. This algorithm uses a seed to generate the series, which should be initialized to some distinctive value using function srand.