How to make numbers from a list of permutations? - python

The program gets 5 random numbers(0-9).I generated permutations into a list.
An example: numbers: 0,1,2,3,4 list = [(1,0,2,3,4),(1,0,2,4,3),...] and I would like to get: list = [10234,10243,...]
And have to make an exception for 0, of course numbers can't start with 0.
Only 5 digit numbers are accepted. So from the example can't get 1234, because 0 must be included everywhere except for the first.
import numpy as np
import itertools
m = np.random.randint(0,10,5)
m = list(m)
print(m)
x = set(itertools.permutations(m))
print(x)

I believe you can simply add:
list(map(int,(''.join(map(str,i)) for i in list(x) if i[0] != 0)))
# [44757, 44577, 47574, 74745, 74475,...

Related

Compare and multiply elements in a list

I'm trying to write in an algorithm a function that:
Check if all elements in a list are different
Multiply all elements in the list, except the zeros
But I can't find a way to compare all elements in one list, do you have any idea ?
Thanks!
PS: I use arr = np.random.randint(10, size=a) to create a random list
EDIT:
More precisely, I'm trying to check if, in a numpy array to be more precise, all the elements are the same or different, if they are different, that it returns me True.
Also, once that done, multiply all elements in the array except the zeros
For example:
If I have an array [4,2,6,8,9,0], the algorithm tells returns me at first True because all elements are different, then it multiplies them 4*2*6*8*9 except the 0
To check if all elements in a list are different you can convert the list into a set which removes duplicates and compare the length of the set to the original list. If the length of the set is different than the length of the list, then there are duplicates.
x = np.random.randint(10, size=10)
len(set(x)) == len(x)
To multiply all values except 0 you can do list comprehension to remove the 0s and use np.prod to multiply the values in the new list.
np.prod([i for i in x if i != 0])
Example:
x = np.random.randint(10, size=10)
if len(set(x)) == len(x):
print(np.prod([i for i in x if i != 0]))
else:
print("array is not unique")
You can use numpy.unique.
Following code snippet checks if all elements in the array are unique (different from each other) and if so, it will multiply non-zero values with factor factor:
import numpy as np
factor = 5
if np.unique(arr).size == arr.size:
arr[arr != 0] = arr[arr != 0] * factor
You can use Collections to find the unique numbers. I have included a code that solves your problem.
import numpy as np
from collections import Counter
a = 5
arr = np.random.randint(10, size=a)
result = 1 #Variable that will store the product
flag = 0 #The counter
#Check if all the numbers are unique
for i in Counter(arr):
if Counter(arr)[i] > 1:
flag = 1
break
#Convert the dictionary into a list
l = [i for i in Counter(arr)]
#Return the product of all the numbers in the list except 0
if flag == 0:
for i in l:
if i != 0:
result = result * i
else:
print("The numbers are not unique")
Just for fun, here's a one-liner:
arr = np.array([1, 2, 3, 4, 0])
np.prod(arr[arr!=0]) if np.unique(arr).size == arr.size else False
>>> 24
If the array is [1, 2, 3, 4, 4] the result is False

Range(5) is returning 4 or 5 random integers

I've created a program to generate 5 random integers from 1-10 and add them to an empty set. For some reason, when I run the program, it will sometimes return 4 integers, and other times 5. What is happening here?
import random
set1 = set()
for x in range(5):
integer = random.randint(1,10)
set1.add(integer)
print(set1)
You're using a set, sets can't contain duplicates, if the same number is generated twice it will only occur in the set once, theoretically its possible your output would only have 1 number in it (if the same number was added 5 times).
You should use a list instead:
import random
output = []
for x in range(5):
integer = random.randint(1,10)
output += [integer]
print(output)
The easiest way to generate n random unique numbers is to use random.sample:
>>> import random
>>> set(random.sample(range(1, 11), 5))
set([8, 9, 5, 6, 10])
Note that you should use range(1, 11) if you also want to include the number 10.
Python sets will not show duplication. A simple way to fix your script is to use a list instead of a set. One thing to note however, is if you are going to want to use all the numbers together like 12345, this won't do the trick. The following script will return a list as [1, 2, 3, 4, 5].
list1 = [] # Make an empty list
for x in range(5):
# randomly choose a number from 1 - 10 and append it to our list 5 times
integer = random.randint(1,10)
list1.append(integer)
print(list1) # print the list

Creating a list of random numbers without duplicates in python

so what I am trying to do is create a list of 5 numbers for the game mastermind, and I would like to eliminate all duplicates! The issue is that the code sometimes creates a list with 3 numbers, or 4, or sometimes 5, it seems to be random.
I should also mention we are not allowed to be usin grandom.sample, or random.shuffle
import random
def generatePassword() :
lis = []
for i in range(5) :
#This checks to see if there are duplicate numbers
r = random.randint(1,9)
if r not in lis :
lis.append(r)
i+=1
return lis
def main() :
print(generatePassword())
main()
Use numpy.random.permutation if you are looking for method that works and is faster:
import numpy as np
your_list = list(np.random.permutation(np.arange(0,10))[:5])
>>> your_list
[6, 9, 0, 1, 4]
Alternatively, you can use np.random.choice with replace=False:
your_list = list(np.random.choice(np.arange(0,10), 5, replace=False)
Try using a while loop with a condition that checks for the length of lis
while len(lis) < 5:
instead of your for loop
The function random.sample does what you want:
import random
def generatePassword():
numbers = range(0, 9)
return random.sample(numbers, 5)
def main() :
print(generatePassword())
main()
I do not recommend the solution in this answer - the best option in the standard library is probably random.sample, and there may be more efficient methods using numpy. Both of these options are suggested in other answers.
This method uses random.shuffle to shuffle a list of digits, then selects the first five. This avoids the issue of a theoretically unbounded loop (while len(nums) < 5:), but does not scale well when the range of numbers to choose from (here, 1 to 9) is significantly larger than how many numbers are needed (here, 5).
import random
population = list(range(1, 10))
random.shuffle(population)
print(population[:5])
You don't want to add random, unique integers 5 times; you want to add random, unique integers until your list contains 5 elements. This'll do it:
import random
def generatePassword() :
lis = []
while len(lis) < 5:
#This checks to see if there are duplicate numbers
r = random.randint(1,9)
if r not in lis :
lis.append(r)
return lis
So your problem:
It won't add the same number twice. But since you use a for i in range(5): it will only repeat 5 times, regardless of if it added a unique number or not.
You need to measure the length of the list, so it will always add 5 random, unique numbers to the list.
You have the code mostly right, but all you need to do is replace:
for i in range(5): with: while len(lis) < 5:
Make sure to delete the i += 1 though. It will cause an error if you don't.
Here's the code:
import random
def generatePassword() :
lis = []
while len(lis) < 5:
#This checks to see if there are duplicate numbers
r = random.randint(1,9)
if r not in lis :
lis.append(r)
return lis
def main() :
print(generatePassword())
main()

Generating n random numbers between i and j python

I want to create a random number using n numbers which are between i and j. For instance, for n=10 and i=1 and j=5, such an output is expected: 2414243211. I did it in R using this code:
paste(floor(runif(10,1,5)),collapse="") #runif create 10 random number between 1 and 5 and floor make them as integer and finally paste makes them as a sequence of numbers instead of array.
I want to do the same in Python. I found random.uniform but it generates 1 number and I don't want to use loops.
import random
import math
math.floor(random.uniform(1,5)) #just generate 1 number between 1 and 5
update:
i and j are integers between 0 and 9, while n could be any integer.
i and j decide which number can be used in the string while n indicates the length of the numeric string.
If I understand your question (not sure I do), and you have Python 3.6, you can use random.choices:
>>> from random import choices
>>> int(''.join(map(str, choices(range(1, 5), k=10))))
2121233233
The random.choices() function does what you want:
>>> from random import choices
>>> n, i, j = 10, 1, 5
>>> population = list(map(str, range(i, j+1)))
>>> ''.join(choices(population, k=n))
'5143113531'
If you consider list-comprehensions being loops (which they actually in many ways are) there you will not be satisfied with this but I will try my luck:
from random import randint
res = ''.join([str(randint(1, 5)) for _ in range(10)])
print(res) #-> 4353344154
Notes:
The result is a string! If you want an integer, cast to int.
randint works incluselively; the start (1) and end (5) might be produced and returned. If you do not want that, modify them (start = 2 and end = 4)
Is there a reason you are using random.uniform (and subsequently math.floor()) and not simply randint?
x = ''.join([str(math.floor(random.uniform(1,5))) for i in range(10)])

How to generate a random number with a specific amount of digits?

Let's say I need a 3-digit number, so it would be something like:
>>> random(3)
563
or
>>> random(5)
26748
>> random(2)
56
You can use either of random.randint or random.randrange. So to get a random 3-digit number:
from random import randint, randrange
randint(100, 999) # randint is inclusive at both ends
randrange(100, 1000) # randrange is exclusive at the stop
* Assuming you really meant three digits, rather than "up to three digits".
To use an arbitrary number of digits:
from random import randint
def random_with_N_digits(n):
range_start = 10**(n-1)
range_end = (10**n)-1
return randint(range_start, range_end)
print random_with_N_digits(2)
print random_with_N_digits(3)
print random_with_N_digits(4)
Output:
33
124
5127
If you want it as a string (for example, a 10-digit phone number) you can use this:
n = 10
''.join(["{}".format(randint(0, 9)) for num in range(0, n)])
If you need a 3 digit number and want 001-099 to be valid numbers you should still use randrange/randint as it is quicker than alternatives. Just add the neccessary preceding zeros when converting to a string.
import random
num = random.randrange(1, 10**3)
# using format
num_with_zeros = '{:03}'.format(num)
# using string's zfill
num_with_zeros = str(num).zfill(3)
Alternatively if you don't want to save the random number as an int you can just do it as a oneliner:
'{:03}'.format(random.randrange(1, 10**3))
python 3.6+ only oneliner:
f'{random.randrange(1, 10**3):03}'
Example outputs of the above are:
'026'
'255'
'512'
Implemented as a function that can support any length of digits not just 3:
import random
def n_len_rand(len_, floor=1):
top = 10**len_
if floor > top:
raise ValueError(f"Floor '{floor}' must be less than requested top '{top}'")
return f'{random.randrange(floor, top):0{len_}}'
Does 0 count as a possible first digit? If so, then you need random.randint(0,10**n-1). If not, random.randint(10**(n-1),10**n-1). And if zero is never allowed, then you'll have to explicitly reject numbers with a zero in them, or draw n random.randint(1,9) numbers.
Aside: it is interesting that randint(a,b) uses somewhat non-pythonic "indexing" to get a random number a <= n <= b. One might have expected it to work like range, and produce a random number a <= n < b. (Note the closed upper interval.)
Given the responses in the comments about randrange, note that these can be replaced with the cleaner random.randrange(0,10**n), random.randrange(10**(n-1),10**n) and random.randrange(1,10).
You could write yourself a little function to do what you want:
import random
def randomDigits(digits):
lower = 10**(digits-1)
upper = 10**digits - 1
return random.randint(lower, upper)
Basically, 10**(digits-1) gives you the smallest {digit}-digit number, and 10**digits - 1 gives you the largest {digit}-digit number (which happens to be the smallest {digit+1}-digit number minus 1!). Then we just take a random integer from that range.
import random
fixed_digits = 6
print(random.randrange(111111, 999999, fixed_digits))
out:
271533
You could create a function who consumes an list of int, transforms in string to concatenate and cast do int again, something like this:
import random
def generate_random_number(length):
return int(''.join([str(random.randint(0,10)) for _ in range(length)]))
I really liked the answer of RichieHindle, however I liked the question as an exercise. Here's a brute force implementation using strings:)
import random
first = random.randint(1,9)
first = str(first)
n = 5
nrs = [str(random.randrange(10)) for i in range(n-1)]
for i in range(len(nrs)) :
first += str(nrs[i])
print str(first)
From the official documentation, does it not seem that the sample() method is appropriate for this purpose?
import random
def random_digits(n):
num = range(0, 10)
lst = random.sample(num, n)
print str(lst).strip('[]')
Output:
>>>random_digits(5)
2, 5, 1, 0, 4
I know it's an old question, but I see many solutions throughout the years. here is my suggestion for a function that creates an n digits random string with default 6.
from random import randint
def OTP(n=6):
n = 1 if n< 1 else n
return randint(int("1"*n), int("9"*n))
of course you can change "1"*n to whatever you want the start to be.

Categories

Resources