5

I know there are plenty of random number generators out there, but I am looking for something that may be a little more predictable. Is there a way to get a "random" number (one that would be the same in every instance) given a string? I would like to do this in bash. I am looking for something a little more advanced than the count of characters in the string, but not as advanced as a full checksum of it.

The end goal is to get a decimal value, so this could be ran against a string multiple times repeating the result.

7
  • 5
    Why not use a hash function like md5sum or sha1sum? echo -n "Hello World" | md5sum. Commented Sep 1, 2011 at 0:28
  • I agree, a hash is probably easiest; I use one for a similar purpose myself. Commented Sep 1, 2011 at 0:35
  • You said "not as advanced as a full checksum". Why? sha1sum is easy enough to use. Commented Sep 1, 2011 at 0:45
  • 1
    In what range? What are you going to use the number for? Commented Sep 1, 2011 at 0:48
  • So a checksum is the best way to do this? I was hoping to get a decimal value, so couldn't the checksum value get turned into a decimal? Commented Sep 1, 2011 at 0:56

3 Answers 3

7

You need a random number, but you don't want a full checksum, it's contradiction. I think md5sum and sha1sum is really easy to use and should fit your needs:

md5sum <<< "$your_str"

or

sha1sum <<< "$your_str"

Update:

If you need decimal numbers, just:

n=$(md5sum <<< "$your_str")
echo $((0x${n%% *}))
Sign up to request clarification or add additional context in comments.

2 Comments

Just what I'm looking for. Thanks Mu.
md5sum <<< "$your_str" uses the <<< herestring bash functionality. ${n%% *} extracts the first of the 2 fields returned by md5sum, which is the md5 hash. It does this by deleting the longest match of the substring " *" from the end of the variable. $((0x________)) converts the hexadecimal md5sum from hex (base 16) to decimal (base 10)
7

To hash the string abc to 3 decimals:

echo $((0x$(echo abc | md5sum | cut -f 1 -d " " | cut -c 1-3))) | cut -c 1-3

To get e.g. 4 decimals instead of 3, replace the two occurrences of 3 by 4:

echo $((0x$(echo abc | md5sum | cut -f 1 -d " " | cut -c 1-4))) | cut -c 1-4
  • $(echo abc | md5sum | cut -f 1 -d " " | cut -c 1-4) gives an hexadecimal of 4 digits.
  • $((0xHEX)) converts an hexadecimal (HEX) to decimal, which may have more than 4 digits even when HEX has 4 digits, e.g. ffff is 65535.
  • cut -c 1-4 gets the first 4 digits.

Comments

2
$ echo abc | md5sum | grep -Eo "[[:digit:]]{3}"  | head -n1
Result -> 248
$ echo xyz | md5sum | grep -Eo "[[:digit:]]{3}"  | head -n1
Result -> 627

Combined other answers!

For HEX colors

echo abc | sha1sum | grep -Eo "[a-f0-9]{6}"  | head -n1
03cfd7
echo abc | sha256sum | grep -Eo "[a-f0-9]{6}"  | head -n1
edeaaf
echo abc | sha512sum | grep -Eo "[a-f0-9]{6}"  | head -n1
4f285d

1 Comment

The first solution for 3 digits fails in (rare) cases where the hash has no 3 consecutive decimals. See my solution for an approach that always returns 3 decimals.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.