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
355
votes
11 answers

Shuffle an array with python, randomize array item order with python

What's the easiest way to shuffle an array with python?
davethegr8
  • 11,323
  • 5
  • 36
  • 61
338
votes
18 answers

Better way to shuffle two numpy arrays in unison

I have two numpy arrays of different shapes, but with the same length (leading dimension). I want to shuffle each of them, such that corresponding elements continue to correspond -- i.e. shuffle them in unison with respect to their leading…
Josh Bleecher Snyder
  • 8,262
  • 3
  • 35
  • 37
337
votes
19 answers

How can I shuffle the lines of a text file on the Unix command line or in a shell script?

I want to shuffle the lines of a text file randomly and create a new file. The file may have several thousands of lines. How can I do that with cat, awk, cut, etc?
Ruggiero Spearman
  • 6,735
  • 5
  • 26
  • 37
328
votes
11 answers

Why do people say there is modulo bias when using a random number generator?

I have seen this question asked a lot but never seen a true concrete answer to it. So I am going to post one here which will hopefully help people understand why exactly there is "modulo bias" when using a random number generator, like rand() in…
user1413793
  • 9,057
  • 7
  • 30
  • 42
321
votes
14 answers

Random float number generation

How do I generate random floats in C++? I thought I could take the integer rand and divide it by something, would that be adequate enough?
hasen
  • 161,647
  • 65
  • 194
  • 231
317
votes
19 answers

How do I generate random numbers in Dart?

How do I generate random numbers using Dart?
Seth Ladd
  • 112,095
  • 66
  • 196
  • 279
307
votes
9 answers

Why does rand() + rand() produce negative numbers?

I observed that rand() library function when it is called just once within a loop, it almost always produces positive numbers. for (i = 0; i < 100; i++) { printf("%d\n", rand()); } But when I add two rand() calls, the numbers generated now have…
badmad
  • 2,059
  • 2
  • 14
  • 14
298
votes
23 answers

Generate a random point within a circle (uniformly)

I need to generate a uniformly random point within a circle of radius R. I realize that by just picking a uniformly random angle in the interval [0 ... 2π), and uniformly random radius in the interval (0 ... R) I would end up with more points…
aioobe
  • 413,195
  • 112
  • 811
  • 826
297
votes
14 answers

How to randomly select rows in SQL?

I am using MSSQL Server 2005. In my db, I have a table "customerNames" which has two columns "Id" and "Name" and approx. 1,000 results. I am creating a functionality where I have to pick 5 customers randomly every time. Can anyone tell me how to…
djmzfKnm
  • 26,679
  • 70
  • 166
  • 227
296
votes
25 answers

How can I get a random number in Kotlin?

A generic method that can return a random integer between 2 parameters like ruby does with rand(0..n). Any suggestion?
Yago Azedias
  • 4,480
  • 3
  • 17
  • 31
291
votes
12 answers

How to access random item in list?

I have an ArrayList, and I need to be able to click a button and then randomly pick out a string from that list and display it in a messagebox. How would I go about doing this?
jay_t55
  • 11,362
  • 28
  • 103
  • 174
291
votes
24 answers

Random alpha-numeric string in JavaScript?

What's the shortest way (within reason) to generate a random alpha-numeric (uppercase, lowercase, and numbers) string in JavaScript to use as a probably-unique identifier?
Pavel
  • 5,320
  • 8
  • 35
  • 45
288
votes
2 answers

How can I generate random number in specific range in Android?

I want to generate random number in a specific range. (Ex. Range Between 65 to 80) I try as per below code, but it is not very use full. It also returns the value greater then max. value(greater then 80). Random r = new Random(); int i1 =…
Mohit Kanada
  • 15,274
  • 8
  • 31
  • 41
283
votes
32 answers

Generating Random Passwords

When a user on our site loses his password and heads off to the Lost Password page we need to give him a new temporary password. I don't really mind how random this is, or if it matches all the "needed" strong password rules, all I want to do is…
FryHard
  • 10,305
  • 7
  • 35
  • 38
282
votes
31 answers

Random shuffling of an array

I need to randomly shuffle the following Array: int[] solutionArray = {1, 2, 3, 4, 5, 6, 6, 5, 4, 3, 2, 1}; Is there any function to do that?
Hubert
  • 16,012
  • 18
  • 45
  • 51