Skip to content

Instantly share code, notes, and snippets.

@danchen6
Created January 16, 2025 14:36
Show Gist options
  • Select an option

  • Save danchen6/c5171f990f7aec2a26274f7f9a697214 to your computer and use it in GitHub Desktop.

Select an option

Save danchen6/c5171f990f7aec2a26274f7f9a697214 to your computer and use it in GitHub Desktop.
Generate a random 64-bit signed integer, with timestamp embedded in the higher 42 bits.
import random
import time
def random_int64():
'''Generate a random 64-bit signed integer, with timestamp embedded in the higher 42 bits.'''
CUSTOM_EPOCH = 1735689600 # 2025/01/01 00:00:00 UTC
HIGH_42BIT_MASK = 0x7FFFFFFFFFC00000 # hex((2 ** 41 - 1) << 22)
LOW_22BIT_MASK = 0x3FFFFF # hex(2 ** 22 - 1)
time_offset_ms = int((time.time() - CUSTOM_EPOCH) * 1000)
random_bits = random.getrandbits(22)
high_bits = (time_offset_ms << 22) & HIGH_42BIT_MASK
low_bits = random_bits & LOW_22BIT_MASK
return high_bits | low_bits
if __name__ == '__main__':
import unittest
import unittest.mock as mock
def _get_time_part(value):
return value >> 22
def _get_random_part(value):
return value & (2 ** 22 - 1)
class TestRandomInt64(unittest.TestCase):
def test_return_type_is_int(self):
value = random_int64()
self.assertIsInstance(value, int)
def test_return_signed_64bits(self):
value = random_int64()
self.assertGreaterEqual(value, -2**63)
self.assertLessEqual(value, 2**63 - 1)
def test_lower_22_bits_randomness(self):
random_parts = [_get_random_part(random_int64()) for _ in range(10)]
unique_valueues = len(set(random_parts))
self.assertGreater(unique_valueues, 1)
def test_higher_42_bits_timestamp(self):
time_part1 = _get_time_part(random_int64())
time_part2 = _get_time_part(random_int64())
self.assertLessEqual(time_part1, time_part2)
def test_time_mocked(self):
with mock.patch('time.time', return_value=1800000000.0):
value1 = random_int64()
value2 = random_int64()
self.assertEqual(_get_time_part(value1), _get_time_part(value2))
self.assertNotEqual(_get_random_part(value1), _get_random_part(value2))
def test_getrandbits_mocked(self):
with mock.patch('random.getrandbits', return_value=0x3AAAAA):
value = random_int64()
self.assertEqual(_get_random_part(value), 0x3AAAAA)
def test_time_before_custom_epoch(self):
# The function will produce a negative offset in the top 42 bits,
# which is still a valid signed 64-bit integer, but we ensure no error is thrown.
with mock.patch('time.time', return_value=1735689600.0 - 100):
value = random_int64()
self.assertGreaterEqual(value, -2**63)
self.assertLessEqual(value, 2**63 - 1)
def test_high_42bit_mask_is_respected(self):
# Verify the function doesn't overflow beyond the 42-bit shift.
mock_time_offset_sec = (2**42 - 1) / 1000 + 1735689600
with mock.patch('time.time', return_value=mock_time_offset_sec):
value = random_int64()
self.assertLessEqual(_get_time_part(value), (2**42 - 1))
def test_multiple_calls_no_errors(self):
# Call the function multiple times to ensure it does not raise exceptions.
# If there's any performance or memory issue, it might show up here.
for _ in range(100000):
_ = random_int64()
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment