PostgreSQL PL/pgSQL random value from array of values
Try this one: select (array[‘Yes’, ‘No’, ‘Maybe’])[floor(random() * 3 + 1)];
Try this one: select (array[‘Yes’, ‘No’, ‘Maybe’])[floor(random() * 3 + 1)];
Change the line where you define selectedexpression to selectedexpression=${expressions[ $RANDOM % ${#expressions[@]} ]} You want your index into expression to be a random number from 0 to the length of the expression array. This will do that.
The simplest (and hence best) C++ (using the 2011 standard) answer is: #include <random> std::random_device rd; // Only used once to initialise (seed) engine std::mt19937 rng(rd()); // Random-number engine used (Mersenne-Twister in this case) std::uniform_int_distribution<int> uni(min,max); // Guaranteed unbiased auto random_integer = uni(rng); There isn’t any need to reinvent the wheel, worry about bias, or … Read more
This is a variant on Hank Ditton’s suggestion that should be more efficient time- and memory-wise, especially if you’re selecting relatively few points out of all possible points. The idea is that, whenever a new point is generated, everything within 200 units of it is added to a set of points to exclude, against which … Read more
That’s not “the last digit” of the number. That’s the last digit of the string str gave you when passed the number. When you call str on a float, Python gives you enough digits that calling float on the string will give you the original float. For this purpose, a trailing 1 or 9 is … Read more
Pick a 8 or 9 digit number at random, say 839712541. Then, take your order number’s binary representation (for this example, I’m not using 2’s complement), pad it out to the same number of bits (30), reverse it, and xor the flipped order number and the magic number. For example: 1 = 000000000000000000000000000001 Flip = … Read more
Use std::random_device to generate the seed. It’ll provide non-deterministic random numbers, provided your implementation supports it. Otherwise it’s allowed to use some other random number engine. std::mt19937_64 prng; seed = std::random_device{}(); prng.seed(seed); operator() of std::random_device returns an unsigned int, so if your platform has 32-bit ints, and you want a 64-bit seed, you’ll need to … Read more
You will get statistical bias when you use modulo (%) to map the range of e.g. rand() to another interval. E.g suppose rand() maps uniformly (without bias) to [0, 32767] and you want to map to [0,4] doing rand() % 5. Then the values 0, 1, and 2 will on average be produced 6554 out … Read more
Figured this out, here’s a function that does it: CREATE OR REPLACE FUNCTION generate_uid(size INT) RETURNS TEXT AS $$ DECLARE characters TEXT := ‘ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789’; bytes BYTEA := gen_random_bytes(size); l INT := length(characters); i INT := 0; output TEXT := ”; BEGIN WHILE i < size LOOP output := output || substr(characters, get_byte(bytes, i) % l … Read more
I don’t know what the reason is, but if you move -lm to the end, it will compile. $ cc -o randomselection rfc3797.c MD5.c -lm rfc3797.c: In function ‘getinteger’: rfc3797.c:183:3: warning: format not a string literal and no format arguments [-Wformat-security]