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?

31246 questions
3731
votes
68 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? I have tried the following, but those do not work: Attempt 1: randomNum = minimum + (int)(Math.random() * maximum); Bug: randomNum can be bigger than maximum. Attempt 2: Random rn = new…
user42155
  • 44,999
  • 25
  • 56
  • 59
2169
votes
39 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
  • 22,297
  • 4
  • 16
  • 15
2156
votes
80 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
  • 75,197
  • 69
  • 188
  • 262
2118
votes
32 answers

How do I generate a random int number?

How do I generate a random integer in C#?
Rella
  • 59,216
  • 102
  • 341
  • 614
2050
votes
29 answers

Generate random number between two numbers in JavaScript

Is there a way to generate a random number in a specified range (e.g. from 1 to 6: 1, 2, 3, 4, 5, or 6) in JavaScript?
Mirgorod
  • 26,593
  • 17
  • 47
  • 62
1972
votes
15 answers

How to randomly select an item from a list?

Assume I have the following list: foo = ['a', 'b', 'c', 'd', 'e'] What is the simplest way to retrieve an item at random from this list?
Ray
  • 169,974
  • 95
  • 213
  • 200
1831
votes
44 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
  • 18
  • 11
1818
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
  • 19,267
  • 12
  • 39
  • 53
1518
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
  • 55,858
  • 96
  • 259
  • 471
1493
votes
58 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?
Click Upvote
  • 235,452
  • 251
  • 553
  • 736
1482
votes
32 answers

Random string generation with upper case letters and digits

I want to generate a string of size N. It should be made up of numbers and uppercase English letters such as: 6U1S75 4Z4UKK U911K4 How can I achieve this in a pythonic way?
Hellnar
  • 51,929
  • 70
  • 190
  • 269
1122
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
  • 35,491
  • 52
  • 126
  • 159
1098
votes
33 answers

How can I generate random alphanumeric strings?

How can I generate a random 8 character alphanumeric string in C#?
KingNestor
  • 59,315
  • 50
  • 115
  • 149
944
votes
25 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
  • 9,631
  • 3
  • 15
  • 8
878
votes
62 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
  • 9,199
  • 4
  • 17
  • 17
1
2 3
99 100