# Random number generator, range ignored

Hello,

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’m not sure if this is technically correct, but I’d use this:

```Dim rand As New Random Dim r As Int16 = rand.InRange( -32768, 32767 )```

Using Int16 should theoretically prevent you from getting non-16-bit values.

I think it should be 0, 65535

For what purpose will you be using these random bits? If they need to be cryptographically secure, you should use the Crypto module instead.

``dim r as UInt16 = Crypto.GetRandomBytes( 2 ).UInt16Value( 0 )``

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.

Just my 2¢.

[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.

And it doesn’t appear to be cryptographically secure, right?

It does not

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.