Python- How to generate random integers with multiple ranges?

后端 未结 6 1980
忘掉有多难
忘掉有多难 2021-01-12 01:25

I\'ve run into confusion in generating X amount of random integers from different sets of (a,b). For example, I would like to generate 5 random integers coming from (1,5), (

相关标签:
6条回答
  • 2021-01-12 01:31
    for i in (randint(1,5),randint(9,15),randint(21,27)):
        print i
    

    This foor loop will generate 3 random numbers one from the first range supplied, another for second range and last one for last range and prints every one on output. Yours print is out of the foor loop and prints only the last random number from the last range given.

    0 讨论(0)
  • 2021-01-12 01:39

    for non repeated numbers:

    from random import randint, choice
    
    randoms = []
    counter = 0    
    while True:
       new_random = (choice([randint(1,5),randint(9,15),randint(21,27)]))
       if new_random not in randoms:
          randoms.append(new_random)
          counter += 1
       if counter == 5 :
          break
    print randoms
    
    0 讨论(0)
  • 2021-01-12 01:40

    Hi!

    This is an interesting question; it becomes interesting when you realize that to achieve true randomness, the probability of picking a particular range must be weighed by the length of that range.

    Ranges of Equal Length:

    If the three ranges are of equal length, say range(0, 10), range(20, 30) and range(40, 50); then, to pick a single random number, we may do the following:

    1. Pick a range at random.
    2. Pick a random number from that range.

    Ranges of Unequal Length:

    Now, consider three of unequally sized ranges, say range(0, 2), range(4, 6) and range(10, 100);

    The third range is much larger than the first two. If we employ the same strategy we employed in dealing with equally long ranges, we will be biased towards picking numbers from the first two ranges.

    In order to pick truly random numbers from the three unequally long ranges, there are two strategies.

    Strategy 1: Using probability

    The probability of picking a range should be such that the probability of picking a number remains the same. We could accomplish this by weighing down the probability of piking shorter ranges.

    However, instead of computing probability weights; there's a better solution. See Strategy 2.

    Strategy 2: Merging the ranges

    We could simply merge the three ranges into a single single range. Then, randomly pick a number from the merged range. It's simple:

    import random;
    def randomPicker(howMany, *ranges):
        mergedRange = reduce(lambda a, b: a + b, ranges);
        ans = [];
        for i in range(howMany):
            ans.append(random.choice(mergedRange));
        return ans;
    

    Let's see it in action:

    >>> randomPicker(5, range(0, 10), range(15, 20), range(40, 60));
    [47, 50, 4, 50, 16]
    >>> randomPicker(5, range(0, 10), range(70, 90), range(40, 60));
    [0, 9, 55, 46, 44]
    >>> randomPicker(5, range(0, 10), range(40, 60));
    [50, 43, 7, 42, 4]
    >>> 
    

    An added benefit of randomPicker is that it can deal with any number of ranges.

    Hope this helps.

    0 讨论(0)
  • 2021-01-12 01:42

    There are some interesting answers here, though I think this problem can be solved with a bit less code, even if it is a little bit less readable.

    The basic idea here is that you have a fixed number of choices so you can essentially have one range to do the job and then plot the result to the ranges you want. Or if you want to consider it another way, create a function f(x) -> y that boils down to the same thing.

    from random import randint
    
    for i in xrange(5):
        n = randint(1,19)
        if n > 12:
            print(n + 8)
        elif n > 5:
            print(n + 3)
        else:
            print(n)
    

    Or, with a function:

    from random import randint
    
    def plot_to_ranges(n):
        if n > 12:
            return n + 8
        elif n > 5:
            return n + 3
        else:
            return n
    
    for i in xrange(5):
        n = randint(1,19)
        print(plot_to_ranges(n))
    

    If you're using Python 3.x, you should change xrange to range.

    0 讨论(0)
  • 2021-01-12 01:42
    import itertools, random
    nums = list(itertools.chain(
                range(1,5),
                range(9,15),
                range(21,27)))
    random.choices(nums, k=5)
    
    0 讨论(0)
  • 2021-01-12 01:52

    Not ideal

    from random import randint, choice
    
    for _ in range(5):
        print(choice([randint(1,5),randint(9,15),randint(21,27)]))
    

    As Blender said - clearer version

    from random import randint, choice
    
    for _ in range(5):
        r = choice([(1,5),(9,15),(21,27)])
        print(randint(*r))
    
    0 讨论(0)
提交回复
热议问题