Questions tagged [random]
This tag is for questions pertaining to random numbers and their generators, whether pseudo-random or truly random.
random
35,697
questions
4098
votes
59
answers
5.2m
views
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 ...
2824
votes
94
answers
2.9m
views
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?
2533
votes
33
answers
2.5m
views
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.
2522
votes
41
answers
2.0m
views
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?
2416
votes
32
answers
3.1m
views
How do I generate a random integer in C#?
How do I generate a random integer in C#?
2369
votes
18
answers
2.2m
views
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']
2069
votes
58
answers
1.6m
views
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?
1973
votes
46
answers
1.7m
views
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 ...
1903
votes
14
answers
223k
views
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:
...
1882
votes
23
answers
2.8m
views
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
1751
votes
36
answers
1.4m
views
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
1493
votes
13
answers
1.5m
views
Get a random item from a JavaScript array [duplicate]
var items = Array(523, 3452, 334, 31, ..., 5346);
How do I get random item from items?
1312
votes
39
answers
1.2m
views
How can I generate random alphanumeric strings?
How can I generate a random 8 character alphanumeric string in C#?
1255
votes
28
answers
1.1m
views
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?
1016
votes
69
answers
1.8m
views
PHP random string generator
I'm trying to create a randomized string in PHP, and I get absolutely no output with this:
<?php
function RandomString()
{
$characters = '...
934
votes
26
answers
1.1m
views
Shuffling a list of objects [duplicate]
How do I shuffle a list of objects? I tried random.shuffle:
import random
b = [object(), object()]
print(random.shuffle(b))
But it outputs:
None
845
votes
28
answers
99k
views
Understanding "randomness"
I can't get my head around this, which is more random?
rand()
OR:
rand() * rand()
I´m finding it a real brain teaser, could you help me out?
EDIT:
Intuitively I know that the mathematical answer ...
841
votes
15
answers
249k
views
Random number generator only generating one random number
I have the following function:
//Function to get random number
public static int RandomNumber(int min, int max)
{
Random random = new Random();
return random.Next(min, max);
}
How I call it:
...
834
votes
18
answers
574k
views
How to get a random number in Ruby
How do I generate a random number between 0 and n?
829
votes
46
answers
520k
views
How to generate a random string in Ruby
I'm currently generating an 8-character pseudo-random uppercase string for "A" .. "Z":
value = ""; 8.times{value << (65 + rand(25)).chr}
but it doesn't look clean, and it can't be passed as ...
754
votes
13
answers
469k
views
Generating random numbers in Objective-C
I'm a Java head mainly, and I want a way to generate a pseudo-random number between 0 and 74. In Java I would use the method:
Random.nextInt(74)
I'm not interested in a discussion about seeds or ...
718
votes
79
answers
225k
views
Expand a random range from 1–5 to 1–7
Given a function which produces a random integer in the range 1 to 5, write a function which produces a random integer in the range 1 to 7.
709
votes
33
answers
2.4m
views
How to generate a random int in C?
Is there a function to generate a random int number in C? Or will I have to use a third party library?
676
votes
7
answers
765k
views
How to get a random number between a float range?
random.randrange(start, stop) only takes integer arguments. So how would I get a random number between two float values?
647
votes
7
answers
338k
views
How do I pick randomly from an array?
I want to know if there is a much cleaner way of doing this. Basically, I want to pick a random element from an array of variable length. Normally, I would do it like this:
myArray = ["stuff", "...
608
votes
32
answers
587k
views
How to request a random row in SQL?
How can I request a random row (or as close to truly random as possible) in pure SQL?
596
votes
22
answers
380k
views
Seeding the random number generator in Javascript
Is it possible to seed the random number generator (Math.random) in JavaScript?
572
votes
65
answers
911k
views
Random color generator
Given this function, I want to replace the color with a random color generator.
document.overlay = GPolyline.fromEncoded({
color: "#0000FF",
weight: 10,
points: encoded_points,
...
563
votes
2
answers
2.6m
views
Getting random numbers in Java [duplicate]
I would like to get a random value between 1 to 50 in Java.
How may I do that with the help of Math.random();?
How do I bound the values that Math.random() returns?
554
votes
29
answers
581k
views
MySQL select 10 random rows from 600K rows fast
How can I best write a query that selects 10 rows randomly from a total of 600k?
541
votes
26
answers
431k
views
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?
497
votes
13
answers
385k
views
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 ...
496
votes
31
answers
806k
views
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, ...
489
votes
27
answers
436k
views
How to generate random number in Bash?
How to generate a random number within a range in Bash?
488
votes
20
answers
425k
views
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?
453
votes
7
answers
466k
views
Create a GUID / UUID in Java
What are some of the best ways to create a GUID / UUID in Java?
445
votes
30
answers
226k
views
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 ...
440
votes
13
answers
680k
views
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?
401
votes
28
answers
362k
views
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 ...
377
votes
17
answers
421k
views
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 ...
374
votes
19
answers
490k
views
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, ...
373
votes
7
answers
584k
views
Produce a random number in a range using C#
How do I go about producing random numbers within a range?
372
votes
10
answers
307k
views
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 ...
371
votes
9
answers
240k
views
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?
369
votes
11
answers
351k
views
Shuffle an array with python, randomize array item order with python
What's the easiest way to shuffle an array with python?
369
votes
4
answers
612k
views
Generate 'n' unique random numbers within a range [duplicate]
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):
...
343
votes
18
answers
205k
views
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 ...
343
votes
19
answers
208k
views
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?
332
votes
20
answers
318k
views
How do I generate random numbers in Dart?
How do I generate random numbers using Dart?
332
votes
10
answers
72k
views
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 ...