From: Stephen Hemminger <redacted>
Date: Thu, 04 Oct 2012 14:05:26 -0700 (PDT)
quoted
From: Stephen Hemminger <redacted>
Date: Thu, 4 Oct 2012 13:56:56 -0700
quoted
- first = (((u64)rand * remaining) >> 32) + low;
+ first = rand % remaining + low;
I really don't get it, either a modulus via multiplcation
works or it doesn't. We have this construct all over the
place.
Try out of kernel:
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
int main(int ac, char **av) {
int low, high, remaining;
unsigned int rand;
unsigned short first;
low = atoi(av[1]);
high = atoi(av[2]);
remaining = (high - low) + 1;
rand = atoi(av[3]);
first = (((uint64_t)rand * remaining) >> 32) + low;
printf("%d %d %u => %u\n", low, high, rand, first);
return 0;
}
$ port-test 32768 61000 $RANDOM
32768 61000 7027 => 32768
It just shows that we need to shift "remaining" up into the top N
bits instead of the bottom N bits.