Yeah I'm not a math wiz and the older I get the harder I find maths.
But sometimes you can't evade maths in programming.

So here's my question for math wizards.
Essentially, I'm trying to write an url shortener.
And I found this package I agree with (don't re-invent the wheel)
https://github.com/neptulon/shortid

It base64-encodes a random number of [bits that need to be dividable by 8] 
length.
Dividable by 8 because 1 char = 8 bits.

It generates an input+1/8 length result.

What I like is, get a count of all links in the db, easy.
But now the problem.

8bit = 64^2 possible results without overlapping., 2 characters (therefore 
64^2)
16bit = 64^3 etc
up to 64bit with would equal an uint64 value

My problem is computing the formula.
if link_count > number_bits/2 {
  number_bits++
}

but how do I find out the bits?
and make it future proof (yes even with the size of uint64)
aka I must not define ranges e.g. >=0 <=(64^2)/2
because I'm afraid the value would overflow in Go. (64^9)

I could
exponent := 2 // starts at 2 because bits = (exponent-1)*8
result := link_count / (64^exponent)
if result < 1.0 {
  number_bits = exponent-1
  if result < 0.5 {
    good_to_generate = true
  } else {
    exponent++
  }
}

but it would still probably overflow

However... MongoDB (my backing database), at least the mgo driver returns 
an int, which on 64bit machines is int64.
So there's my limit. Let's hope "int128" will be enough.

I guess I answered my own question. I'll still post it, because it took 
some time to write.

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to