# Mid-Square hashing

Mid-Square hashing is a hashing technique in which unique keys are generated. In this technique, a seed value is taken and it is squared. Then, some digits from the middle are extracted. These extracted digits form a number which is taken as the new seed. This technique can generate keys with high randomness if a big enough seed value is taken. However, it has a limitation. As the seed is squared, if a 6-digit number is taken, then the square will have 12-digits. This exceeds the range of int data type. So, overflow must be taken care of. In case of overflow, use long long int data type or use string as multiplication if overflow still occurs. The chances of a collision in mid-square hashing are low, not obsolete. So, in the chances, if a collision occurs, it is handled using some hash map.

**Example**:

Suppose a 4-digit seed is taken. seed = 4765

Hence, square of seed is = 4765 * 4765 = 22705225

Now, from this 8-digit number, any four digits are extracted (Say, the middle four).

So, the new seed value becomes seed = 7052

Now, square of this new seed is = 7052 * 7052 = 49730704

Again, the same set of 4-digits is extracted.

So, the new seed value becomes seed = 7307

.

.

.

.

This process is repeated as many times as a key is required.

Mid square technique takes a certain number of digits from the square of a number. This number extracted is a pseudo-random number, which can be used as a key for hashing.

**Algorithm:**

- Choose a seed value. This is an important step as for same seed value, the same sequence of random numbers is generated.
- Take the square of the seed value and update seed by a certain number of digits that occur in the square.
**Note**: The larger is the number of digits, larger will be the randomness. - Return the key.

Below is the implementation of above algorithm:

`// C++ program to illustrate the ` `// mid-square hashing technique ` `#include <ctime> ` `#include <iostream> ` ` ` `using` `namespace` `std; ` ` ` `// Returns a seed value based on current system time. ` `long` `long` `int` `newTime() ` `{ ` ` ` ` ` `// Acquiring number of seconds ` ` ` `// passed from Jan 1, 1970. ` ` ` `time_t` `t = ` `time` `(NULL); ` ` ` ` ` `// Converting the time to year, month, ` ` ` `// day, hour, minute, and second. ` ` ` `struct` `tm` `* ` `tm` `= ` `localtime` `(&t); ` ` ` `long` `long` `int` `x; ` ` ` ` ` `// Applying a certain logic to get ` ` ` `// required number of digits. It may vary. ` ` ` `x = (` `tm` `->tm_hour) * 10000000 + (` `tm` `->tm_min) * 100000 ` ` ` `+ (` `tm` `->tm_sec) * 1000 + (` `tm` `->tm_mday) * 10 + (` `tm` `->tm_year); ` ` ` ` ` `// Return the calculated number. ` ` ` `return` `x; ` `} ` ` ` `// Returns a random 8-digit key. ` `long` `int` `getKey() ` `{ ` ` ` ` ` `// Taking the key from system time. ` ` ` `// returns a 8-digit seed value. ` ` ` `static` `long` `long` `int` `key = newTime(); ` ` ` ` ` `// Squaring the key. ` ` ` `key = key * key; ` ` ` ` ` `// Extracting required number of digits ( here, 8 ). ` ` ` `if` `(key < 1000000000000000) { ` ` ` `key = key / 10000; ` ` ` `key = key % 100000000; ` ` ` `} ` ` ` `else` `{ ` ` ` `key = key / 10000; ` ` ` `key = key % 100000000; ` ` ` `} ` ` ` ` ` `// Returning the key value. ` ` ` `return` `key; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `// get the first key ` ` ` `std::cout << getKey() << endl; ` ` ` ` ` `// get the second key ` ` ` `std::cout << getKey() << endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

56002270 25424515

**Note: **The output will change according to the date and time.

## Recommended Posts:

- Hashing in Distributed Systems
- Number of subarrays having sum of the form k^m, m >= 0
- Total distinct pairs of ugly numbers from two arrays
- Number of ways in which the substring in range [L, R] can be formed using characters out of the range
- Count sub-arrays whose product is divisible by k
- Maximum count of pairs which generate the same sum
- Number of pairs from the first N natural numbers whose sum is divisible by K
- Count pairs in array whose sum is divisible by K
- Maximize the number of sum pairs which are divisible by K
- Print all the sum pairs which occur maximum number of times
- Total character pairs from two strings, with equal number of set bits in their ascii value
- Count all elements in the array which appears at least K times after their first occurrence
- Generate a random permutation of elements from range [L, R] (Divide and Conquer)
- Generate a random permutation of 1 to N

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.