I think your confusion is that you do not know what a hash is. Most languages have something similar to a repository of key values; in Ruby and Perl they are called hashes, on Java maps, in Python dictionaries, etc.
All of them are essentially the same thing, you insert a value with a unique key into some basic data structure to get direct access to it at the expense of memory.
So what happens when you add a key and value to a hash?
Hashes are built around the idea of hash functions that take on some value, since input calculates a unique result (ideally, each input has its own unique output). If two inputs coincide with the same output, this is called a collision.
Now we are at the point where we need to talk about how Hash is implemented, two classic examples - with one array or an array of linked lists. I will show an example of an array below.
Array
In the case of a simple array, the data structure underlying the Hash is an array of some size. The hash function is used to calculate the index into this array. If we adopt a simple hashing algorithm
h(x) = length(x) % ARRAY_SIZE
here x is a string, and ARRAY_SIZE is the size of our base array, this operator will ensure that all x values fall in the range 0..ARRAY_SIZE - 1
To look at a visual example, consider an array of size 5:
0 1 2 3 4 ------------------------------ | | | | | | ------------------------------
and suppose we are trying to save the value 5 with the abcd key, according to our hash algorithm
h('abcd') = length('abcd') % ARRAY_SIZE = 4 % 5 = 4
So, the value 5 will be stored in index 4 :
0 1 2 3 4 ------------------------------ | | | | | 5 | ------------------------------
Now, what happens if we try to save the value 3 with the dcba key, these two keys will be different? They should be displayed in different places.
h('dcba') = length('dcba') % ARRAY_SIZE = 4 % 5 = 4
Oops! This key also displays index 4 , and what will we do now? Well, we can't just throw away the key-value pair, because the programmer obviously needs / needs this to pair in their Hash, so we need to decide what to do in the event of a collision. There are many algorithms that do this, but the easiest is to search for the next open slot in the array and save 3 them. So now our array looks like this:
0 1 2 3 4 ------------------------------ | 3 | | | | 5 | ------------------------------
This was not an extremely detailed explanation, but hopefully it will give some insight into why getting values from hashes seems random, because the underlying data structure is constantly changing, if you were to ask for the keys to your hash right now you are likely to return (3, 5) , even if you inserted 5 in the first place, just because 3 occurs first in the array.
Hope this was helpful.