Select n random rows from SQL Server table

select top 10 percent * from [yourtable] order by newid() In response to the “pure trash” comment concerning large tables: you could do it like this to improve performance. select * from [yourtable] where [yourPk] in (select top 10 percent [yourPk] from [yourtable] order by newid()) The cost of this will be the key scan … Read more

Weighted random numbers

There is a straightforward algorithm for picking an item at random, where items have individual weights: 1) calculate the sum of all the weights 2) pick a random number that is 0 or greater and is less than the sum of the weights 3) go through the items one at a time, subtracting their weight … Read more

Math.random() versus Random.nextInt(int)

Here is the detailed explanation of why “Random.nextInt(n) is both more efficient and less biased than Math.random() * n” from the Sun forums post that Gili linked to: Math.random() uses Random.nextDouble() internally. Random.nextDouble() uses Random.next() twice to generate a double that has approximately uniformly distributed bits in its mantissa, so it is uniformly distributed in … Read more

Why is the use of rand() considered bad?

There are two parts to this story. First, rand is a pseudorandom number generator. This means it depends on a seed. For a given seed it will always give the same sequence (assuming the same implementation). This makes it not suitable for certain applications where security is of a great concern. But this is not … Read more

Recommended way to initialize srand?

This is what I’ve used for small command line programs that can be run frequently (multiple times a second): unsigned long seed = mix(clock(), time(NULL), getpid()); Where mix is: // Robert Jenkins’ 96 bit Mix Function unsigned long mix(unsigned long a, unsigned long b, unsigned long c) { a=a-b; a=a-c; a=a^(c >> 13); b=b-c; b=b-a; … Read more

How to generate a random number in C++?

Using modulo may introduce bias into the random numbers, depending on the random number generator. See this question for more info. Of course, it’s perfectly possible to get repeating numbers in a random sequence. Try some C++11 features for better distribution: #include <random> #include <iostream> int main() { std::random_device dev; std::mt19937 rng(dev()); std::uniform_int_distribution<std::mt19937::result_type> dist6(1,6); // … Read more