random

How to request a random row in SQL?

What is the best way to request a random row in pure SQL? ...

Select a random N elements from List<T> in C#

I need a quick algorithm to select a random 5 elements from a generic list. For example, I'd like to get a random 5 elements from a List. ...

Arithmetic underflow or overflow exception during debugging

This is the day of weird behavior. We have a Win32 project made with Delphi 2007, which hosts the .NET runtime and calls into .NET to show new forms, as part of a transition period. Recently we've begun experiencing exceptions at seemingly random locations and points of our code: Arithmetic overflow or underflow. The stack trace of on...

Random weighted choice

Consider the class below that represents a Broker: public class Broker { public string Name = string.Empty; public int Weight = 0; public Broker(string n, int w) { this.Name = n; this.Weight = w; } } I'd like to randomly select a Broker from an array, taking into account their weights. What do you...

Random Weighted Choice in T-SQL

How do you randomly select a table row in T-SQL based on an applied weight for all candidate rows? For example, I have a set of rows in a table weighted at 50, 25, and 25 (which adds up to 100 but does not need to), and I want to select one of them randomly with a statistical outcome equivalent to the respective weight. ...

Converting a Uniform Distribution to a Normal Distribution

How can I convert a uniform distribution (as most random number generators produce, e.g. between 0.0 and 1.0) into a normal distribution? What if I want a mean and standard deviation of my choosing? ...

Unit test for randomized data

I ran across this situation this afternoon, so I thought I'd ask what you guys do. We have a randomized password generator for user password resets and while fixing a problem with it, I decided to move the routine into my (slowly growing) test harness. I want to test that passwords generated conform to the rules we've set out, but of c...

How best to generate a random string in Ruby

I'm currently using the following to generate an 8 character pseudo random upper case string [A-Z] value = ""; 8.times{value << (65 + rand(25)).chr} but it looks junky, and since it isn't a single statement it can't be passed as an argument. To get a mixed case string [a-zA-Z] I further hack into it with value = ""; 8.times{value <<...

Create many constrained, random permutation of a list

I need to make a random list of permutations. The elements can be anything but assume that they are the integers 0 through x-1. I want to make y lists, each containing z elements. The rules are that no list may contain the same element twice and that over all the lists, the number of times each elements is used is the same (or as clos...

How do I return random numbers as a column in SQL Server 2005?

I'm running a SQL query on SQL Server 2005, and in addition to 2 columns being queried from the database, I'd also like to return 1 column of random numbers along with them. I tried this: select column1, column2, floor(rand() * 10000) as column3 from table1 ...which kinda works, but the problem is that this query returns the same rand...

What is the difference between a randomly generated number and secure randomly generated number?

As the title says What is the difference between a randomly generated number and secure randomly generated number? ...

How do you generate passwords?

Random Characters? Passphrases? High Ascii? cat /dev/urandom | strings ...

How can I give java.util.Random a specific seed in thirdparty classes?

I have a Java program that loads thirdparty class files (classes I did not write) and executes them. These classes often use java.util.Random, which by default generates random starting seed values every time it gets instantiated. For reasons of reproducability, I want to give these classes the same starting seed every time, changing it ...

Best way to randomize a string array in C#

I was just wondering what is the best way to randomize an array of strings in C#. My array contains about 500 strings and I'd like to create a new Array with the same strings but in a random order. Any suggestions? ...

What is the best replacement for Windows' rand_s in Linux/POSIX?

The problem is not about randomness itself (we have rand), but in cryptographically secure PRNG. What can be used on Linux, or ideally POSIX? Does NSS have something useful? Clarification: I know about /dev/random, but it may run out of entropy pool. And I'm not sure whether /dev/urandom is guaranteed to by cryptographically secure. ...

Testing for Random Value - Thoughts on this Approach?

OK, I have been working on a random image selector and queue system (so you don't see the same images too often). All was going swimmingly (as far as my crappy code does) until I got to the random bit. I wanted to test it, but how do you test for it? There is no Debug.Assert(i.IsRandom) (sadly) :D So, I got my brain on it after waterin...

Picking a random element from a set

How do I pick a random element from a set? I'm particularly interested in picking a random element from a HashSet or a LinkedHashSet, in Java. Solutions for other languages are also welcome. ...

Is reading /dev/urandom thread-safe ?

This is the code: unsigned int number; FILE* urandom = fopen("/dev/urandom", "r"); if (urandom) { size_t bytes_read = fread(&number, 1, sizeof(number), urandom); DCHECK(bytes_read == sizeof(number)); fclose(urandom); } else { NOTREACHED(); } If not, how do I make it thread-safe? ...

best way to pick a random subset from a collection?

I have a set of objects in a Vector from which I'd like to select a random subset (e.g. 100 items coming back; pick 5 randomly). In my first (very hasty) pass I did an extremely simple and perhaps overly clever solution: Vector itemsVector = getItems(); Collections.shuffle(itemsVector); itemsVector.setSize(5); While this has the adva...

What is a cross platform way to select a random seed in Java?

After reading this answer: http://stackoverflow.com/questions/136474/best-way-to-pick-a-random-subset-from-a-collection#136513 It got me wondering, how does one pick a random seed in Java? And don't say use System.currentTimeMillis() or System.nanoTime(). Read the article to see why not. That's a hard question, but let me make it har...