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
277
votes
6 answers

Random row selection in Pandas dataframe

Is there a way to select random rows from a DataFrame in Pandas. In R, using the car package, there is a useful function some(x, n) which is similar to head but selects, in this example, 10 rows at random from x. I have also looked at the slicing…
John
  • 41,131
  • 31
  • 82
  • 106
270
votes
13 answers

What's an easy way to read random line from a file?

What's an easy way to read random line from a file in a shell script?
Newbie Prog
263
votes
14 answers

How to generate a random number in C++?

I'm trying to make a game with dice, and I need to have random numbers in it (to simulate the sides of the die. I know how to make it between 1 and 6). Using #include #include #include using namespace std; int…
Predictability
  • 2,919
  • 4
  • 16
  • 12
252
votes
10 answers

JS generate random boolean

Simple question, but I'm interested in the nuances here. I'm generating random booleans using the following method I came up with myself: const rand = Boolean(Math.round(Math.random())); Whenever random() shows up, it seems there's always a pitfall…
Ben
  • 54,723
  • 49
  • 178
  • 224
247
votes
4 answers

Get random item from array

$items = Array(523,3452,334,31,...5346); Each item of this array is some number. How do I get random item from $items?
James
  • 42,081
  • 53
  • 136
  • 161
243
votes
7 answers

Random number between 0 and 1?

I want a random number between 0 and 1, like 0.3452. I used random.randrange(0, 1) but it is always 0 for me. What should I do?
Talia
  • 2,947
  • 4
  • 17
  • 28
243
votes
20 answers

Get a random number focused on center

Is it possible to get a random number between 1-100 and keep the results mainly within the 40-60 range? I mean, it will go out of that range rarely, but I want it to be mainly within that range... Is it possible with JavaScript/jQuery? Right now I'm…
Darryl Huffman
  • 2,499
  • 3
  • 18
  • 40
241
votes
12 answers

How to properly seed random number generator

I am trying to generate a random string in Go and here is the code I have written so far: package main import ( "bytes" "fmt" "math/rand" "time" ) func main() { fmt.Println(randomString(10)) } func randomString(l int) string…
copperMan
  • 2,435
  • 2
  • 14
  • 5
233
votes
13 answers

Generate random numbers with a given (numerical) distribution

I have a file with some probabilities for different values e.g.: 1 0.1 2 0.05 3 0.05 4 0.2 5 0.4 6 0.2 I would like to generate random numbers using this distribution. Does an existing module that handles this exist? It's fairly simple to code on…
pafcu
  • 7,808
  • 12
  • 42
  • 55
232
votes
32 answers

Generating a random password in php

I am trying to generate a random password in php. However I am getting all 'a's and the return type is of type array and I would like it to be a string. Any ideas on how to correct the code? Thanks. function randomPassword() { $alphabet =…
nunos
  • 20,479
  • 50
  • 119
  • 154
229
votes
30 answers

Random String Generator Returning Same String

I've developed a random string generator but it's not behaving quite as I'm hoping. My goal is to be able to run this twice and generate two distinct four character random strings. However, it just generates one four character random string…
PushCode
  • 3,077
  • 4
  • 25
  • 26
228
votes
14 answers

Random / noise functions for GLSL

As the GPU driver vendors don't usually bother to implement noiseX in GLSL, I'm looking for a "graphics randomization swiss army knife" utility function set, preferably optimised to use within GPU shaders. I prefer GLSL, but code any language will…
Kos
  • 70,399
  • 25
  • 169
  • 233
228
votes
3 answers

Generating a Random Number between 1 and 10 Java

I want to generate a number between 1 and 10 in Java. Here is what I tried: Random rn = new Random(); int answer = rn.nextInt(10) + 1; Is there a way to tell what to put in the parenthesis () when calling the nextInt method and what to add?
Shania
  • 2,475
  • 3
  • 13
  • 11
228
votes
7 answers

Difference between java.util.Random and java.security.SecureRandom

My team got handed over some server side code (in Java) that generates random tokens and I have a question regarding the same - The purpose of these tokens is fairly sensitive - used for session id, password reset links etc. So they do need to be…
user967973
  • 2,475
  • 2
  • 18
  • 13
224
votes
12 answers

random.seed(): What does it do?

I am a bit confused on what random.seed() does in Python. For example, why does the below trials do what they do (consistently)? >>> import random >>> random.seed(9001) >>> random.randint(1, 10) 1 >>> random.randint(1, 10) 3 >>> random.randint(1,…
Ahaan S. Rungta
  • 2,323
  • 2
  • 12
  • 15