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
4073
votes
73 answers

How do I generate random integers within a specific range in Java?

How do I generate a random int value in a specific range? The following methods have bugs related to integer overflow: randomNum = minimum + (int)(Math.random() * maximum); // Bug: `randomNum` can be bigger than `maximum`. Random rn = new…
user42155
  • 48,965
  • 27
  • 59
  • 60
2751
votes
93 answers

Generate random string/characters in JavaScript

I want a 5 character string composed of characters picked randomly from the set [a-zA-Z0-9]. What's the best way to do this with JavaScript?
Tom Lehman
  • 85,973
  • 71
  • 200
  • 272
2490
votes
40 answers

Generating random whole numbers in JavaScript in a specific range

How can I generate random whole numbers between two specified variables in JavaScript, e.g. x = 4 and y = 8 would output any of 4, 5, 6, 7, 8?
zacharyliu
  • 25,578
  • 4
  • 21
  • 15
2476
votes
32 answers

Generate random number between two numbers in JavaScript

Is there a way to generate a random number in a specified range with JavaScript ? For example: a specified range from 1 to 6 were the random number could be either 1, 2, 3, 4, 5, or 6.
Mirgorod
  • 31,413
  • 18
  • 51
  • 63
2377
votes
33 answers

How do I generate a random integer in C#?

How do I generate a random integer in C#?
Rella
  • 65,003
  • 109
  • 363
  • 636
2333
votes
18 answers

How can I randomly select (choose) an item from a list (get a random element)?

How do I retrieve an item at random from the following list? foo = ['a', 'b', 'c', 'd', 'e']
Ray
  • 187,153
  • 97
  • 222
  • 204
1993
votes
71 answers

How to randomize (shuffle) a JavaScript array?

I have an array like this: var arr1 = ["a", "b", "c", "d"]; How can I randomize / shuffle it?
Ali
  • 261,656
  • 265
  • 575
  • 769
1958
votes
46 answers

How to generate a random alpha-numeric string

I've been looking for a simple Java algorithm to generate a pseudo-random alpha-numeric string. In my situation it would be used as a unique session/key identifier that would "likely" be unique over 500K+ generation (my needs don't really require…
Todd
  • 3,363
  • 3
  • 21
  • 12
1891
votes
15 answers

Why does this code using random strings print "hello world"?

The following print statement would print "hello world". Could anyone explain this? System.out.println(randomString(-229985452) + " " + randomString(-147909649)); And randomString() looks like this: public static String randomString(int i) { …
0x56794E
  • 20,883
  • 13
  • 42
  • 58
1851
votes
22 answers

Generate random integers between 0 and 9

How can I generate random integers between 0 and 9 (inclusive) in Python? For example, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
aneuryzm
  • 63,052
  • 100
  • 273
  • 488
1717
votes
36 answers

Random string generation with upper case letters and digits

How do I generate a string of size N, made of numbers and uppercase English letters such as: 6U1S75 4Z4UKK U911K4
Hellnar
  • 62,315
  • 79
  • 204
  • 279
1459
votes
13 answers

Get a random item from a JavaScript array

var items = Array(523, 3452, 334, 31, ..., 5346); How do I get random item from items?
James
  • 42,081
  • 53
  • 136
  • 161
1295
votes
37 answers

How can I generate random alphanumeric strings?

How can I generate a random 8 character alphanumeric string in C#?
KingNestor
  • 65,976
  • 51
  • 121
  • 152
1221
votes
28 answers

Getting a random value from a JavaScript array

Consider: var myArray = ['January', 'February', 'March']; How can I select a random value from this array using JavaScript?
Sarah
  • 12,401
  • 3
  • 17
  • 8
1003
votes
69 answers

PHP random string generator

I'm trying to create a randomized string in PHP, and I get absolutely no output with this:
Captain Lightning
  • 10,493
  • 4
  • 19
  • 17
1
2 3
99 100