I’m trying to convert a python script into a xojo app(i’m not that good at python ).
the function i need to convert is random.getrandbits(16)
I didn’t find a direct equivalent in the docs(random class has just a range option). What is the best way to create an equivalent? Do you have any examples i can work with?
I can’t find official documentation from Python that points to the proper implementation for the language but many languages (including C) center the range of a 16 bit integer over 0. C uses the range -32,767 to 32,767, some languages utilize -32,768 to 32,767 and some use 0 to 65,535.
In documentation for some third-party Python libraries (SciPy for example) indicate a 16 bit integer in Python uses the range -32768 to 32,767, but like I said, it really depends on what @Matteo Saitta needs.
As a rule of thumb, when dealing with bits, I prefer unsigned integers. It avoids surprises with the sign bit, especially when using regular math to perform bitwise operations.
[quote=426976:@Kem Tekinay]As a rule of thumb, when dealing with bits, I prefer unsigned integers. It avoids surprises with the sign bit, especially when using regular math to perform bitwise operations.
Just my 2¢.[/quote]
Fair enough, I am inclined to agree with you, but just in case anyone is wondering it does appear the that Random.GetRandomBits() function in Python returns a signed integer of the given bit-length.
Thanks for the replies, i need it to create a random filler uid for a web transaction. It doesn’t need to be crypto secure but it cannot be negative so the UINT16 is preferable for me.