0

I need help with generating a list of specific random numbers using Python.

I know how to generate a list of random number like this

import random
number = random.sample(xrange(1,10), 3)

which will generate 3 random numbers between 1 to 10. (Example)

[7, 6, 1]

What I want to do achieve is generating a list of random numbers like this with duplicate value(Example)

[10000, 30000, 20000, 60000, 100, 3000, 3000, 100, ...]

But using above script like this

import random
number = random.sample(xrange(10000,100000), 4)

yields following result (Example)

[12489, 43177, 51867, 68831]

Is it possible to do what I want to achieve here? Thank your answer.

5
  • 2
    So you want to round to the nearest 100? Or only keep 1 significant figure? Commented Feb 24, 2017 at 4:17
  • 2
    It is unclear what you want... a single digit followed by between 2 and 4 0s? Commented Feb 24, 2017 at 4:19
  • @mgilson @Julien For example, if I put range between 100 until 1000, it will generate random number of [100, 200, 300, 100, 1000, 800,...]. It does not have to be 2 or 4 0s, it can be one 0 or five 0s and more based on the range. Commented Feb 24, 2017 at 4:23
  • If you are generating random numbers you cannot expect to get duplicates - you may and you may not depending on the range and quantity of random numbers. Commented Feb 24, 2017 at 4:28
  • @wwii thank you for that advice. Commented Feb 24, 2017 at 4:34

3 Answers 3

2

Are you looking for random numbers rounded to the thousands?

import random
numbers = [1000*x for x in random.sample(xrange(1,100), 4)]
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. Your suggestion is simpler and easy to understand. And yes, I am looking for random numbers rounded to the thousands.
2

Something that looks like your example

import random
n1 = random.sample(xrange(1,10), 4)
n2 = random.sample(xrange(2,6), 4)
numbers = [n1i * 10**n2i for n1i, n2i in zip(n1, n2)]

Sample output:

[800000, 7000, 200, 30000]

If you want repeats:

random.choices(numbers, k=6)

Sample output:

[800000, 7000, 800000, 800000, 7000, 200]

3 Comments

Definitely curious if this is what the OP wanted.
Thank you, this is exactly what I wanted to do but failed to find any example of it online but between your suggestion and @James suggestion, his suggestion seems simpler. Anyway thank you for your help.
@wwii I find it often the quickest to offer them something and then let them complain ;-)
0

Try this:

import numpy as np
array1 = np.random.randint(8, size=10)
array2 = np.random.randint(6, size=10)
array3 = (np.power(10, array2)) * array1

Arrays 'array1', 'array2' and 'array3' are (examples):

array1 = [2 3 6 7 5 1 1 5 5 7]
array2 = [2 4 1 3 4 5 2 2 4 2]
array3 = [200 30000 60 7000 50000 100000 100 500 50000 700]

array3 is what you want.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.