Chris Stewart - 5 months ago 31

Scala Question

I'm trying to right a good number generator that covers

`uint64_t`

`def uInt64s : Gen[BigInt] = Gen.choose(0,64).map(pow2(_) - 1)`

It is a good start, but it only generates numbers

`2^n - 1`

`0 <= n < 2^64`

Answer

Okay, maybe I am missing something here, but isn't it as simple as this?

```
def uInt64s : Gen[BigInt] = Gen.chooseNum(Long.MinValue,Long.MaxValue)
.map(x => BigInt(x) + BigInt(2).pow(63))
```

Longs already have the correct number of bits - just adding 2^63 so `Long.MinValue`

becomes 0 and `Long.MaxValue`

becomes 2^64 - 1. And doing the addition with `BigInt`

s of course.

I was curious about the distribution of generated values. Apparently the distribution of `chooseNum`

is not uniform, since it prefers special values, but the edge cases for Longs are probably also interesting for UInt64s:

```
/** Generates numbers within the given inclusive range, with
* extra weight on zero, +/- unity, both extremities, and any special
* numbers provided. The special numbers must lie within the given range,
* otherwise they won't be included. */
def chooseNum[T](minT: T, maxT: T, specials: T*)(
```