Questions tagged [random]

This tag is for questions pertaining to random numbers and their generators, whether pseudo-random or truly random.

This tag is for questions pertaining to creating random numbers or random distributions: using random number generators (RNGs), creating specific distributions (e.g. Gaussian, Bernoulli or log-normal) with given parameters from normal random numbers, problems and caveats with given implementations in different languages.

Since different languages may use different RNGs, a question with this tag must also be tagged with the language being used.

Frequently-asked questions

  • How can I generate N uniformly-distributed random real numbers that sum to a given value?

  • How can I choose N elements, avoiding duplicates, from a list (or from the integer range 1..M)? This is known as dealing elements, as in dealing playing cards.

  • If I create a new random number generator object for each request, why does it return the same value repeatedly? (Or: only seed the random generator once)

  • How do I generate a random integer value within a range?

35221 questions
531
votes
26 answers

How to generate a random number in Swift?

I realize the Swift book provided an implementation of a random number generator. Is the best practice to copy and paste this implementation? Or is there a library that does this that we can use now?
door_number_three
  • 5,843
  • 2
  • 16
  • 21
487
votes
31 answers

How to generate a random, unique, alphanumeric string?

How would it be possible to generate a random, unique string using numbers and letters for use in a verify link? Like when you create an account on a website, and it sends you an email with a link, and you have to click that link in order to verify…
Andrew
  • 227,796
  • 193
  • 515
  • 708
482
votes
13 answers

Best way to select random rows PostgreSQL

I want a random selection of rows in PostgreSQL, I tried this: select * from table where random() < 0.01; But some other recommend this: select * from table order by random() limit 1000; I have a very large table with 500 Million rows, I want it…
nanounanue
  • 7,942
  • 7
  • 41
  • 73
469
votes
19 answers

How to generate a random string of a fixed length in Go?

I want a random string of characters only (uppercase or lowercase), no numbers, in Go. What is the fastest and simplest way to do this?
Anish Shah
  • 7,669
  • 8
  • 29
  • 40
467
votes
26 answers

How to generate random number in Bash?

How to generate a random number within a range in Bash?
woakas
  • 5,106
  • 2
  • 18
  • 8
443
votes
30 answers

How can I get a random record from MongoDB?

I am looking to get a random record from a huge collection (100 million records). What is the fastest and most efficient way to do so? The data is already there and there are no field in which I can generate a random number and obtain a random row.
Will M
  • 4,431
  • 3
  • 16
  • 3
442
votes
7 answers

Create a GUID / UUID in Java

What are some of the best ways to create a GUID / UUID in Java?
Chris Dutrow
  • 48,402
  • 65
  • 188
  • 258
431
votes
13 answers

Sample random rows in dataframe

I am struggling to find the appropriate function that would return a specified number of rows picked up randomly without replacement from a data frame in R language? Can anyone help me out?
nikhil
  • 9,023
  • 22
  • 55
  • 81
392
votes
28 answers

A weighted version of random.choice

I needed to write a weighted version of random.choice (each element in the list has a different probability for being selected). This is what I came up with: def weightedChoice(choices): """Like random.choice, but each element can have a…
Colin
  • 10,447
  • 11
  • 46
  • 54
375
votes
17 answers

Laravel - Eloquent or Fluent random row

How can I select a random row using Eloquent or Fluent in Laravel framework? I know that by using SQL, you can do order by RAND(). However, I would like to get the random row without doing a count on the number of records prior to the initial…
DigitalWM
  • 4,406
  • 3
  • 18
  • 15
366
votes
18 answers

Select n random rows from SQL Server table

I've got a SQL Server table with about 50,000 rows in it. I want to select about 5,000 of those rows at random. I've thought of a complicated way, creating a temp table with a "random number" column, copying my table into that, looping through the…
John M Gant
  • 18,970
  • 18
  • 64
  • 82
365
votes
7 answers

Produce a random number in a range using C#

How do I go about producing random numbers within a range?
RoR
  • 15,934
  • 22
  • 71
  • 92
362
votes
10 answers

Get a random boolean in python?

I am looking for the best way (fast and elegant) to get a random boolean in python (flip a coin). For the moment I am using random.randint(0, 1) or random.getrandbits(1). Are there better choices that I am not aware of?
Xavier V.
  • 6,068
  • 6
  • 30
  • 35
360
votes
8 answers

Select random lines from a file

In a Bash script, I want to pick out N random lines from input file and output to another file. How can this be done?
user121196
  • 30,032
  • 57
  • 148
  • 198
360
votes
4 answers

Generate 'n' unique random numbers within a range

I know how to generate a random number within a range in Python. random.randint(numLow, numHigh) And I know I can put this in a loop to generate n amount of these numbers for x in range (0, n): listOfNumbers.append(random.randint(numLow,…
Chris Headleand
  • 6,003
  • 16
  • 51
  • 69