1

I want to generate random numbers from two different ranges [0, 0.3) and [0.7, 1) in python.

numpy.random.uniform has the option of generating only from one particular interval.

Ravi Raja
  • 61
  • 9

4 Answers4

4

I assume you want to choose an interval with probability weighted by its size, then sample uniformly from the chosen interval. In that case, the following Python code will do this:

import random

# Define the intervals.  They should be disjoint.
intervals=[[0, 0.05], [0.7, 1]]
# Choose one number uniformly inside the set
random.uniform(*random.choices(intervals,
   weights=[r[1]-r[0] for r in intervals])[0])

import numpy

# Generate a NumPy array of given size
size=1000
numpy.asarray([ \
    random.uniform(*random.choices(intervals,
        weights=[r[1]-r[0] for r in intervals])[0]) \
    for i in range(1000)])

Note that the intervals you give, [[0, 0.3], [0.7, 1]], appear to be arbitrary; this solution works for any number of disjoint intervals, and it samples uniformly at random from the union of those intervals.

Peter O.
  • 32,158
  • 14
  • 82
  • 96
  • The obtained values does not look _uniformly_ random enough. Using `sns.kdeplot([random.uniform(*random.choices(intervals, weights=[r[1]-r[0] for r in intervals])[0]) for _ in range(1000)])`, we see that they are skewed towards the second interval [0.7, 1) . Not sure if thats what OP wants. https://imgur.com/Mv9e6lE – cmbfast Aug 10 '21 at 07:11
  • @cmbfast: I understood the question to ask for a number chosen uniformly at random in the union of two or more intervals. This is what my answer does, by choosing one interval with probability proportional to its size. (Note that the solution's example uses different intervals than the ones given in the question, and I understood those intervals to be arbitrary.) – Peter O. Aug 10 '21 at 07:18
  • ah you used a different interval there. Now this makes sense. If we use OP's interval, your method works as well. – cmbfast Aug 10 '21 at 07:22
  • This just gives one random value. How can I create a numpy array of such random values of a given size? @PeterO. – Ravi Raja Aug 10 '21 at 15:14
1

How about this one?

first_interval = np.array([0, 0.3])
second_interval = np.array([0.7, 1])

total_length = np.ptp(first_interval)+np.ptp(second_interval)
n = 100
numbers = np.random.random(n)*total_length
numbers += first_interval.min()
numbers[numbers > first_interval.max()] += second_interval.min()-first_interval.max()
sams-studio
  • 631
  • 3
  • 10
0

Refer to this thread hope it solves your query. But the answer is as follows :

def random_of_ranges(*ranges):
  all_ranges = sum(ranges, [])
  return random.choice(all_ranges)
print(random_of_ranges(range(65, 90), range(97, 122)))

click here

0

You can just concatenate random numbers from those two intervals.

import numpy as np


rng = np.random.default_rng(12345)
a = rng.uniform(0, 0.3,1000)
b = rng.uniform(0.7, 1,1000)

my_rnd = np.concatenate([a, b])

These look fairly uniform across the two intervals.

Random numbers in two intervals

cmbfast
  • 489
  • 4
  • 9