Questions tagged [random]

This tag is for questions pertaining to random numbers and their generators, whether pseudo-random or truly random.

random
Filter by
Sorted by
Tagged with
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 ...
user42155's user avatar
  • 49.3k
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?
Tom Lehman's user avatar
  • 87.5k
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.
Mirgorod's user avatar
  • 32k
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?
zacharyliu's user avatar
  • 25.9k
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#?
Rella's user avatar
  • 65.9k
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']
Ray's user avatar
  • 190k
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?
Ali's user avatar
  • 265k
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: ...
0x56794E's user avatar
  • 21.1k
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
aneuryzm's user avatar
  • 63.9k
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
Hellnar's user avatar
  • 63.5k
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?
James's user avatar
  • 42.9k
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#?
KingNestor's user avatar
  • 66.8k
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?
Sarah's user avatar
  • 12.7k
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 = '...
Captain Lightning's user avatar
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
utdiscant's user avatar
  • 11.3k
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 ...
Trufa's user avatar
  • 40.5k
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: ...
Ivan Prodanov's user avatar
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?
Mark A. Nicolosi's user avatar
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 ...
rustyshelf's user avatar
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?
Kredns's user avatar
  • 36.8k
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?
Mantis Toboggan's user avatar
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", "...
Paul Hoffer's user avatar
  • 12.7k
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?
sverrejoh's user avatar
  • 16.7k
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?
weepy's user avatar
  • 6,481
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, ...
n00ki3's user avatar
  • 14.7k
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?
Unknown user's user avatar
  • 44.9k
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?
Francisc's user avatar
  • 78.9k
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?
door_number_three's user avatar
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 ...
nanounanue's user avatar
  • 8,112
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, ...
Andrew's user avatar
  • 233k
489 votes
27 answers
436k views

How to generate random number in Bash?

How to generate a random number within a range in Bash?
woakas's user avatar
  • 5,326
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?
Anish Shah's user avatar
  • 7,881
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?
Chris Dutrow's user avatar
  • 49.6k
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 ...
Will M's user avatar
  • 4,451
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?
nikhil's user avatar
  • 9,203
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 ...
Colin's user avatar
  • 10.6k
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 ...
DigitalWM's user avatar
  • 4,446
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, ...
John M Gant's user avatar
  • 19.1k
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?
RoR's user avatar
  • 16.2k
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 ...
Xavier V.'s user avatar
  • 6,238
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?
user121196's user avatar
  • 30.5k
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?
davethegr8's user avatar
  • 11.5k
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): ...
Chris Headleand's user avatar
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 ...
Josh Bleecher Snyder's user avatar
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?
Ruggiero Spearman's user avatar
332 votes
20 answers
318k views

How do I generate random numbers in Dart?

How do I generate random numbers using Dart?
Seth Ladd's user avatar
  • 116k
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 ...
user1413793's user avatar
  • 9,187

1
2 3 4 5
714