Questions tagged [uniform-distribution]
159 questions
53
votes
9 answers
How to get a uniform distribution in a range [r1,r2] in PyTorch?
I want to get a 2-D torch.Tensor with size [a,b] filled with values from a uniform distribution (in range [r1,r2]) in PyTorch.

Bishwajit Purkaystha
- 1,975
- 7
- 22
- 30
50
votes
4 answers
Random numbers that add to 100: Matlab
[I'm splitting a population number into different matrices and want to test my code using random numbers for now.]
Quick question guys and thanks for your help in advance -
If I use;
100*rand(9,1)
What is the best way to make these 9 numbers add…

Tetra
- 747
- 2
- 9
- 15
41
votes
10 answers
Sampling uniformly distributed random points inside a spherical volume
I am looking to be able to generate a random uniform sample of particle locations that fall within a spherical volume.
The image below (courtesy of http://nojhan.free.fr/metah/) shows what I am looking for. This is a slice through the sphere,…

Tim McJilton
- 6,884
- 6
- 25
- 27
33
votes
8 answers
Generate a random sample of points distributed on the surface of a unit sphere
I am trying to generate random points on the surface of the sphere using numpy. I have reviewed the post that explains uniform distribution here. However, need ideas on how to generate the points only on the surface of the sphere. I have coordinates…

Parin
- 341
- 1
- 3
- 3
20
votes
7 answers
How do I generate Log Uniform Distribution in Python?
I could not find a built-in function in Python to generate a log uniform distribution given a min and max value (the R equivalent is here), something like: loguni[n, exp(min), exp(max), base] that returns n log uniformly distributed in the range…

jkrish
- 427
- 1
- 5
- 12
14
votes
4 answers
Can random.uniform(0,1) ever generate 0 or 1?
In the documentation it is said that there is a chance that uniform(0,1) can generate the values 0 and 1.
I have run uniform(0, 1) 10000 times, but it never produced zero. Even in the case of uniform(0, 0.001).
Can random.uniform(0,1) ever…

Venkatesh Gandi
- 441
- 1
- 9
- 24
9
votes
1 answer
runif with .Machine$double.xmax as boundaries
I wanted to generate a random real (I guess rational) number.
To do this I wanted to use runif(1, min = m, max = M) and my thoughts were to set m; M (absolute) as large as possible in order to make the interval as large as possible. Which brings me…

niko
- 5,253
- 1
- 12
- 32
7
votes
5 answers
Distribution of Random Numbers
I have two options of code:
Option 1
int myFunc() {
return new Random().nextInt();
}
Or:
Option 2
private static final Random random = new Random();
int myFunc() {
return random.nextInt();
}
I understand that option 2 is more idiomatic. I am…

Benjy Kessler
- 7,356
- 6
- 41
- 69
6
votes
3 answers
Uniform Distribution: Bug or Paradox
Imagine 10 cars randomly, uniformly distributed on a round track of length 1. If the positions are represented by a C double in the range [0,1> then they can be sorted and the gaps between the cars should be the position of the car in front minus…

APA
- 61
- 4
6
votes
2 answers
generate a random point within rectangles' areas uniformly (some rectangles could overlap)
Assume it's given a set of rectangles with different areas and some rectangles could overlap.
Objective is generating of a uniform random point among rectangles' areas.
Rectangle is defined as a pair of two points:
(x1,y1) - bottom leftmost corner;…

Zhandos
- 279
- 1
- 2
- 16
6
votes
1 answer
Do std::random_device and std::mt19937 follow an uniform distribution?
I'm trying to convert this line of matlab in C++: rp = randperm(p);
Following the randperm documentation:
randperm uses the same random number generator as rand
And in rand page:
rand returns a single uniformly distributed random number
So rand…

justHelloWorld
- 6,478
- 8
- 58
- 138
6
votes
2 answers
Will "min to max" uniform real distribution produce Inf,-Inf, or NaN?
If I were to produce floating point values in the following way:
template
T RandomFromRange(T low, T high){
std::random_device random_device;
std::mt19937 engine{random_device()};
std::uniform_real_distribution…

Trevor Hickey
- 36,288
- 32
- 162
- 271
5
votes
1 answer
Getting a random float value in [-1, 1] in Julia
Is
p = rand(-1.:eps():1., 100000)
a good way to get random Float values in [-1, 1]?
A common suggestion seems to be 2. * rand(100000) - 1. instead, but
this doesn't ever return 1 since rand's range is [0, 1)
this skips out on a lot of values:…

Sundar R
- 13,776
- 6
- 49
- 76
5
votes
2 answers
ECDF plot from a truncated MD5
In this link, it says that truncated MD5 is uniformly distributed. I wanted to check it using PySpark and I created 1,000,000 UUIDs in Python first as shown below. Then truncated the first three characters from MD5. But the plot I get is not…

Fisseha Berhane
- 2,533
- 4
- 30
- 48
5
votes
2 answers
Sampling Uniformly within the Unit Circle
I want to sample a two-dimensional vector x from a uniform distribution with ∥x∥ ≤ 1. I know I can sample from a uniform distribution as
numpy.random.uniform(0.0, 1.0, 2)
but how can I make sure ∥x∥ ≤ 1?

user77005
- 1,769
- 4
- 18
- 26