random

How to solve performance problem with Java SecureRandom?

If you want a cryptographically strong random number in Java, you use SecureRandom. Unfortunately, SecureRandom can be very slow. If it uses /dev/random on Linux, it can block waiting for sufficient entropy to build up. How do you avoid the peformance penalty? Has anyone used Uncommon Maths as a solution to this problem? Can anybody co...

Given a function which produces a random integer in the range 1 to 5, write a function which produces a random integer in the range 1 to 7

What is simple solution What is effective solution to less minimum memory and(or) cpu speed? ...

How to simplify this code (generates a random int between min and max base on unsigned int)?

The code is return min + static_cast<int>(static_cast<double>(max - min + 1.0) * (number / (UINT_MAX + 1.0))); number is a random number obtained by rand_s. min and max are ints and represent minimum and maximum values (inclusive). If you provide a solution not using unsigned int as a number, please also explain how to make it be r...

What is the best way to pick a random row from a table in MySQL?

I have seen random rows pulled using queries like this, which are quite inefficient for large data sets. SELECT id FROM table ORDER BY RANDOM() LIMIT 1 I have also seen various other RDBMS-specific solutions that don't work with MySQL. The best thing I can think of doing off-hand is using two queries and doing something like this. ...

Is Windows' rand_s thread-safe?

Just as in title. Is suspect it is, but I couldn't find it anywhere explicitly stated. And for this property I wouldn't like to rely on speculations. ...

How to take the seed for a random generator from the clock, programming in assembler

Compiler, TASM 5 Architecture x86. For a random number generator algorithm, take tha clock as the seed. I found that solution: AcquireSeed PROC PUSH AX PUSH CX PUSH DX MOV Ah, 00h ; Int INT 1AH ; return the clock in CX:DX MOV seed, DL ; save the less significant byte of the clock at seed (declared at .da...

What is the best way to produce random double on POSIX?

I'd like to get uniform distribution in range [0.0, 1.0) If possible, please let the implementation make use of random bytes from /dev/urandom. It would also be nice if your solution was thread-safe. If you're not sure, please indicate that. See some solution I thought about after reading other answers. ...

How do you efficiently generate a list of K non-repeating integers between 0 and an upper bound N

The question gives all necessary data: what is an efficient algorithm to generate a sequence of K non-repeating integers within a given interval. The trivial algorithm (generating random numbers and, before adding them to the sequence, looking them up to see if they were already there) is very expensive if K is large and near enough to N...

Random element in a map

what is a good way to select a random element from a map? C++. It is my understanding that maps don't have random access iterators. The key is a long long and the map is sparsely populated. ...

Iterating shuffled [0..n) without arrays

I know of a couple of routines that work as follows: Xn+1 = Routine(Xn, max) For example, something like a LCG generator: Xn+1 = (a*Xn + c) mod m There isn't enough parameterization in this generator to generate every sequence. Dream Function: Xn+1 = Routine(Xn, max, permutation number) This routine, para...

What am I doing wrong when using RAND() in MS SQL Server 2005?

I'm trying to select a random 10% sampling from a small table. I thought I'd just use the RAND() function and select those rows where the random number is less than 0.10: SELECT * FROM SomeTable WHERE SomeColumn='SomeCondition' AND RAND() < 0.10 But I soon discovered that RAND() always returns the same number! Reminds me of this xkc...

Multiple random values in SQL Server 2005

I need to generate multiple random values under SQL Server 2005 and somehow this simply wont work with Random(Value) as ( select rand() Value union all select rand() from Random )select top 10 * from Random Whats the preffered workaround? ...

How to unit test a pseudo random number generator?

Hi all, I have a pseudo random number generator (PRNG) class that I want to unit test. There are two approaches: write a test case that takes a large amount of samples and test whether they are properly distributed. This approach may lead to a fairly long execution time for the test case; calculate a small series of samples 'by hand'...

Random distribution of data

How do I distribute a small amount of data in a random order in a much larger volume of data? For example, I have several thousand lines of 'real' data, and I want to insert a dozen or two lines of control data in a random order throughout the 'real' data. Now I am not trying to ask how to use random number generators, I am asking a st...

Open source random number generation algorithm in C++?

I need to generate random numbers in the range 1 - 10000 continuously with out duplication. Any recommendations? Description: we are building a new version for our application, which maintains records in Sqlite DB. in the last version of our application, we did not had unique key for each record. But now with new upgraded version, we n...

Windows equivalent of /dev/random

Is there a Windows equivalent of Linux's /dev/random? ...

Random record from a database table (T-SQL)

Is there a succinct way to retrieve a random record from a sql server table? I would like to randomize my unit test data, so am looking for a simple way to select a random id from a table. In English, the select would be "Select one id from the table where the id is a random number between the lowest id in the table and the highest i...

Random date in C#

I'm looking for some succinct C# 3 code to generate a random date between Jan 1 1995 and the current date. I'm thinking some solution that utilizes Enumerable.Range somehow may make this more succinct. ...

Generating random number excluding range

How do you generate a random number within a range while excluding certain range(s). Eg. range 1-10 but not in 2-4 or 7. Solutions I've used so far: Generate a random an test if it is within the dis-allowed range. Based on result either output the number or try again. Map allowed ranges to a uniform range. Get a random between 1 and 6 ...

Unique random numbers in O(1)?

The problem is this: I'd like to generate unique random numbers between 0 and 1000 that never repeat (I.E. 6 doesn't come out twice), but that doesn't resort to something like an O(N) search of previous values to do it. Is this possible? ...