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
222
votes
7 answers

Java Generate Random Number Between Two Given Values

I would like to know how to generate a random number between two given values. I am able to generate a random number with the following: Random r = new Random(); for(int i = 0; i < a.length; i++){ for(int j = 0; j < a[i].length; j++){ …
Mus
  • 7,290
  • 24
  • 86
  • 130
222
votes
18 answers

How can I get a random key-value pair from a dictionary?

In Python, given a dictionary like { 'VENEZUELA': 'CARACAS', 'CANADA': 'OTTAWA' } How can I choose a random item (key-value pair)? What if I only need the key, or only the value - can it be optimized?
tekknolagi
  • 10,663
  • 24
  • 75
  • 119
218
votes
10 answers

Efficient method to generate UUID String in Java (UUID.randomUUID().toString() without the dashes)

I would like an efficient utility to generate unique sequences of bytes. UUID is a good candidate but UUID.randomUUID().toString() generates stuff like 44e128a5-ac7a-4c9a-be4c-224b6bf81b20 which is good, but I would prefer dash-less string. I'm…
Maxim Veksler
  • 29,272
  • 38
  • 131
  • 151
217
votes
16 answers

Pick a random value from an enum?

If I have an enum like this: public enum Letter { A, B, C, //... } What is the best way to pick one randomly? It doesn't need to be production quality bulletproof, but a fairly even distribution would be nice. I could do something…
Nick Heiner
  • 119,074
  • 188
  • 476
  • 699
216
votes
11 answers

How do I select a random value from an enumeration?

Given an arbitrary enumeration in C#, how do I select a random value? (I did not find this very basic question on SO. I'll post my answer in a minute as reference for anyone, but please feel free to post your own answer.)
mafu
  • 31,798
  • 42
  • 154
  • 247
216
votes
34 answers

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.
Clue Less
  • 3,147
  • 2
  • 20
  • 10
214
votes
20 answers

How do I create a random alpha-numeric string in C++?

I'd like to create a random string, consisting of alpha-numeric characters. I want to be able to be specify the length of the string. How do I do this in C++?
jm.
  • 23,422
  • 22
  • 79
  • 93
213
votes
31 answers

Generate a random date between two other dates

How would I generate a random date that has to be between two other given dates? The function's signature should be something like this: random_date("1/1/2008 1:30 PM", "1/1/2009 4:50 AM", 0.34) ^ ^ …
quilby
  • 3,141
  • 4
  • 26
  • 21
210
votes
7 answers

Reasons for using the set.seed function

Many times I have seen the set.seed function in R, before starting the program. I know it's basically used for the random number generation. Is there any specific need to set this?
Vignesh
  • 2,247
  • 2
  • 14
  • 12
208
votes
34 answers

Select N random elements from a List in C#

I need a quick algorithm to select 5 random elements from a generic list. For example, I'd like to get 5 random elements from a List.
JC Grubbs
  • 39,191
  • 28
  • 66
  • 75
208
votes
8 answers

How to generate a random number between a and b in Ruby?

To generate a random number between 3 and 10, for example, I use: rand(8) + 3 Is there a nicer way to do this (something like rand(3, 10)) ?
Misha Moroshko
  • 166,356
  • 226
  • 505
  • 746
207
votes
16 answers

Pick a random element from an array

Suppose I have an array and I want to pick one element at random. What would be the simplest way to do this? The obvious way would be array[random index]. But perhaps there is something like ruby's array.sample? Or if not could such a method be…
Fela
  • 26,730
  • 8
  • 26
  • 24
205
votes
9 answers

Is java.util.Random really that random? How can I generate 52! (factorial) possible sequences?

I've been using Random (java.util.Random) to shuffle a deck of 52 cards. There are 52! (8.0658175e+67) possibilities. Yet, I've found out that the seed for java.util.Random is a long, which is much smaller at 2^64 (1.8446744e+19). From here, I'm…
Sergey Emeliyanov
  • 5,158
  • 6
  • 29
  • 52
202
votes
8 answers

Random state (Pseudo-random number) in Scikit learn

I want to implement a machine learning algorithm in scikit learn, but I don't understand what this parameter random_state does? Why should I use it? I also could not understand what is a Pseudo-random number.
Elizabeth Susan Joseph
  • 6,255
  • 7
  • 20
  • 23
201
votes
8 answers

How can I generate a unique ID in Python?

I need to generate a unique ID based on a random value.
Ryan
  • 11,743
  • 16
  • 37
  • 37