Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to rearrange 36-bit character string using Pseudo-Random Number Generator (PRNG)
    primarykey
    data
    text
    <p>I have made a program to encode and decode long URL's. Encode will take n characters and outputs a 36-bit character string. Decode will take a 36-bit character string and output a long character string.</p> <pre><code>print(decode(encode(1234567890))) '1234567890' </code></pre> <p>So basically, something that will randomize a 36-bit string and it's decode opposite. Is there a way using a pseudo random number generator to make the properties of a seeded PRNG reversible and use some invariant property of the numbers to seed the PRNG.</p> <p>I know bits and pieces of this code, these might help.</p> <pre><code>def ls1b( x ): """Return least significant bit of x that is a one. (Assume x &gt;= 0.)""" return x &amp; -x </code></pre> <p>And</p> <pre><code>def bitson( x ): """Return the number of one bits in x. (Assume x &gt;= 0.)""" count = 0 while x != 0: x &amp;= ~ls1b( x ) count += 1 return count </code></pre> <p>Here is my encode and decode.</p> <pre><code>def token (n): if n &lt; 10: return chr( ord( '0' ) + (n) ) if n in range (10, 36): return chr( ord( 'A' ) - 10 + (n)) if n in range (37, 62): return chr( ord( 'a' ) - 36 + (n)) if n is 62: return '-' if n is 63: return '+' def encode (n): a = n // 1 % 64 b = n // 64 % 64 c = n // 64 ** 2 % 64 d = n // 64 ** 3 % 64 e = n // 64 ** 4 % 64 f = n // 64 ** 5 % 64 return (token(a) + token(b) + token(c) + token(d) + token(e) + token(f)) def tokend (d): x = ord(d) if 48 &lt;= x &lt;= 57: return x - ord('0') if 65 &lt;= x &lt;= 90: return x - ord('A') + 10 if 97 &lt;= x &lt;= 122: return x - ord('a') + 36 if x is 43: return ('62') if x is 45: return ('63') def decode(code, base=64): if base&gt;64: return 'error: too few symbols' code=str(code) o=0 e=0 for i in code: o=o+detoken(i)*base**e e=e+1 while len(str(o))&lt;6: o='0'+str(o) return o </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload