-2
import random
value = 1000
a = []
i = 0
b = [None] * 16
print('value = ',1000)
for x in range(value):
    a.append(x)
    random.Random(4).shuffle(a)
print(a)
for x in range(16):
    b[x] = a[x]
print(b)

This code generate 16 random numbers selected up to the range 1000.But how to generate numbers that has different modulus values from 1 to 26 like this in python ?

Consider the numerical example

the values obtained randomly are: 184,15,106,8,93,150,210,144,271,365,65,60,385,164,349,405 when we perform a mod 26 operation with all these numbers we get 0,15,2,8,15,20,2,14,11,1,13,8,21,8,11,15 respectively

here the numbers 15,8,11,2 are repeating. so i want to eliminate this repeatation. for that i want to generate random numbers that have distinct values while performing a mod 26 operation.

Atul Shanbhag
  • 636
  • 5
  • 13
JwL
  • 43
  • 6
  • 3
    What's "unique modulus values"? – Sheldore Sep 23 '18 at 10:15
  • unique modulus numbers means the modulus values( for example 421 mod 26 ) calculated for each randomly selected numbers should be different or select 16 random numbers those having different modulus values from 1 to 26 as mentioned above. – JwL Sep 23 '18 at 10:30

2 Answers2

1

Suppose you want to find 16 numbers in whose modulo 5 is 4, with maximum value not greater than 1000.

Numpy Solution

def choose_random(max_limit=1000, modulo=5, value=4, size=16):
    x_max = (max_limit - value) // modulo
    if (max_limit - value) % modulo != 0:
        x_max += 1
    x = np.arange(x_max)
    y = x * modulo + value
    return np.random.choice(y, size=size, replace=True)

print(choose_random())
Out: [309 939 449 219 639 614 779 549 189   4 729 629 939 159 934 654] 

Simpler Numpy Solution

def choose_random(max_limit=1000, modulo=5, value=4, size=16):
    y = np.arange(value, max_limit, value)
    return np.random.choice(y, size=size, replace=True)

If you want n different modulo modulo values

def distinct_modulo(n, modulo):
    if n > modulo:
        raise Exception("Can't return more than {0} distinct values!".format(modulo))
    return np.random.choice(modulo, size=n, replace=False)

You simply return n distinct values in range [0, modulo - 1]

distinct_modulo(n=16, modulo=26)
Out: [ 0, 19, 23,  5,  6, 25, 21, 22, 10, 16, 12, 14, 20, 15,  1,  8]

Non Numpy Solution

import random
def distinct_modulo(n, modulo):
    if n > modulo:
        raise Exception("Can't return more than {0} distinct values!".format(modulo))
    return random.sample(range(modulo), n)

distinct_modulo(n=16, modulo=26)     
Out: [14, 17, 13, 10, 1, 6, 0, 20, 2, 21, 4, 19, 9, 24, 25, 16]                                     
Atul Shanbhag
  • 636
  • 5
  • 13
  • The calculated modules values of the selected numbers should be different or each modules value should be unique from another and the modules values can be from the range 1 to 26 – JwL Sep 23 '18 at 11:03
  • It would be better if you could give us a numerical example above – Atul Shanbhag Sep 23 '18 at 11:04
  • I added a simple numerical explanation in the question. hope u understood my question. – JwL Sep 23 '18 at 11:22
  • Here also numbers are repeating in your output. could you please do this without using numpy. – JwL Sep 23 '18 at 11:43
  • The error in the last code was `replace=True` must be `replace=False`. Also added non-numpy solution – Atul Shanbhag Sep 23 '18 at 11:51
0

you can do import random random.sample(range(10000), 1000) this will generate 1000 random numbers

for example to get 10 random number within range 10,000

random.sample(range(10000), 10) [3339, 2845, 9485, 2601, 3569, 1332, 7123, 4861, 9893, 9483]

Prashant
  • 11
  • 3