简体   繁体   中英

How to generate random numbers with if-statement in Python?

I would like to generate random numbers with a specific restriction using python. The code should do the following:

If an entered number is:

0, then generate 0 random non-recurrent numbers
<1, then generate 1 random non-recurrent numbers
<9, then generate 2 random non-recurrent numbers
<15, then generate 3 random non-recurrent numbers
<26, then generate 5 random non-recurrent numbers
<51, then generate 8 random non-recurrent numbers
<91, then generate 13 random non-recurrent numbers
<151, then generate 20 random non-recurrent numbers
<281, then generate 32 random non-recurrent numbers

The value of the random numbers should be limited by the value of the entered number. So if a 75 is entered, then the code should generate 13 random numbers with being 75 the highest value of the 13 numbers. 75 doesn't have to be the actual highest number, just in terms of max value.

My guess was to use numpy. Here is what I got until now (with an users help).

num_files=[0,1,9,...] 
num_nums=[0,1,2,3,5,...]
for zipp in zip(num_files,num_nums)
if len(docx_files)<zipp[0]:
list_of_rands=np.random.choice(len(docx_files)+1, 
zipp[1],replace=False)

Any ideas or more starting points?

Here's one way of doing it. Just zip the lists of numbers and the cutoffs, and check if the number input (the variable number in the code below) is above the cutoff. Note that this doesn't handle the case of numbers larger than 281, since I'm not sure what's supposed to happen there based on your description.

import numpy as np

number = 134
parameters = zip([9, 15, 26, 51, 91, 151], [3, 5, 8, 13, 20, 32])    
nums = 2

for item in parameters:
    if number > item[0]:
        nums = item[1]

np.random.choice(number, nums)

You can avoid a many-branched if-elif-else using np.searchsorted :

import numpy as np

def generate(x):
    boundaries = np.array([1, 2, 9, 15, 26, 51, 91, 151, 281])
    numbers = np.array([0, 1, 2, 3, 5, 8, 13, 20, 32])
    return [np.random.choice(j, n, False)+1 if j else np.array([], np.int64)
            for j, n in np.broadcast(x, numbers[boundaries.searchsorted(x, 'right')])]

# demo
from pprint import pprint
# single value
pprint(generate(17))
# multiple values in one go
pprint(generate([19, 75, 3, 1, 2, 0, 8, 9]))
# interactive
i = int(input('Enter number: '))
pprint(generate(i))

Sample output:

[array([ 9,  1, 14,  4, 12])]
[array([ 8, 12,  6, 17,  4]),
 array([17, 29,  2, 20, 16, 37, 36, 13, 34, 58, 49, 72, 41]),
 array([1, 3]),
 array([1]),
 array([2, 1]),
 array([], dtype=int64),
 array([1, 8]),
 array([3, 2, 6])]
Enter number: 280
[array([184,  73,  80, 280, 254, 164, 192, 145, 176,  29,  58, 251,  37,
       107,   5,  51,   7, 128, 142, 125, 135,  87, 259,  83, 260,  10,
       108, 210,   8,  36, 181,  64])]

I don't know how to implement it in your code but with this code you then you get the randoms:

import random

x = 51

if x < 26:
   ar_Random = [None]*5
   for i in range(0, 6):
      ar_Random[i] = random.randint(startNumOfRandom, stopNumOfRandom)
elif x < 51:
   ar_Random = [None]*8
   for i in range (0,9):
       ar_Random[i] = random.randint(startNumOfRandom, stopNumOfRandom)
...

I'm not sure how you're mapping the length to the input but this is how you generate N random numbers with a maximum using Numpy.

import numpy as np

//set entered_num and desired_length to whatever you want
random_nums = np.random.randint(entered_num, size = desired_length)
import random

Starting_Number = int(input())

if Starting_Number < 26:
    print(random.sample(range(1, 26), 5))

elif Starting_Number < 51:
    print(random.sample(range(1, 51), 8))

elif Starting_Number < 91:
    print(random.sample(range(1, 91), 13))

Here you go!!!

random.sample is the module you are looking for.

Have a good one!

You could define a function using a dictionary with ranges as keys and number of random numbers as values:

import random

def rand_nums(input_num):
    d = {26: 5, 51: 8, 91: 13}

    for k, v in d.items():
        if input_num in range(k):
            nums = random.sample(range(k+1), v)
            return nums



print(rand_nums(20))
print(rand_nums(50))
print(rand_nums(88))

[14, 23, 11, 9, 5]
[9, 49, 23, 16, 8, 50, 47, 33]
[20, 16, 28, 77, 21, 87, 85, 82, 10, 47, 43, 90, 57]
>>> 

How about:

def gen_rand_array(n):
    mapping = np.array([[1,1],
                        [26,5],
                        [51,8],
                        [91,13]])
    k = mapping[np.max(np.where(n > mapping[:,0])),1]
    return np.random.choice(n+1,k)

Example:

>>> gen_rand_array(27)
array([ 0, 21, 26, 25, 23])
>>> gen_rand_array(27)
array([21,  5, 10,  3, 13])
>>> gen_rand_array(57)
array([30, 26, 50, 31, 44, 51, 39, 13])
>>> gen_rand_array(57)
array([21, 18, 35,  8, 13, 13, 20,  3])

Here's a screen shot putting it all together:

在此处输入图片说明

Explanation:

The line k = mapping[np.max(np.where(n > mapping[:,0])),1] is just finding the number of random values needed from the array mapping . n > mapping[:,0] return a boolean array whose values will be True for all the numbers smaller then n , False otherwise. np.where(...) will return the indexes of the elements of the array that are true. Since the values in the first column of mapping (ie mapping[:,0] ) are ascending, we can find the index of the largest one that is less than n be calling np.max(...) . Finally we want the corresponding value from the second column which is why we pass the result of that as an index to mapping again ie mapping[...,1] where the 1 is for the second column.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM