- Related Questions & Answers
- C++ program for hashing with chaining
- Static Perfect Hashing
- Dynamic Perfect Hashing
- Multiple-Choice Hashing
- Find Corners of Rectangle using mid points in C++
- Maximal Square in C++
- Program to print Square inside a Square in C
- Universal Hashing in Data Structure
- Double Hashing in Data Structure
- LCFS Hashing in Data Structure
- Asymmetric Hashing in Data Structure
- Convert an array to reduced form (Hashing) in C++
- Count square and non-square numbers before n in C++
- Program to find the mid-point of a line in C++
- Polybius Square Cipher in C++

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

The mid-square method is a method of generating pseudorandom numbers. This method was invented by John von Neumann and was described at a conference in 1949

In this technique, an initial seed value is taken and it is squared.

Some digits from the middle are extracted and these extracted digits form a number which is taken as the new seed.

Let us take 3456 as seed. Its square is 11943936

Take the middle 4 digits as new seed i.e. 9439. Its square is 89094721

Take middle 4 digits as new seed i.e. 0947

Repeat this process

1. Choose initial seed value 2. Take the square of the seed value 3. Update seed by taking n digits from previous result

#include <iostream> #include <ctime> using namespace std; long long getTime(){ time_t t = time(NULL); struct tm *tm = localtime(&t); long long x = (tm->tm_hour) * 50000000 + (tm->tm_min) * 100000 + (tm->tm_sec) * 5000 + (tm->tm_mday) * 50 + (tm->tm_year); return x; } long getHash(){ long long key = getTime(); key = key * key; key = key / 10000; key = key % 100000000; return key; } int main(){ cout << "Random number: " << getHash() << endl; return 0; }

When you compile and execute the above program. It generates the following output−

Random number: 10088419

Advertisements