I am new to programming and I got stuck with random number generation. I can simply generate random numbers using random function "randint" but could not generate set of random numbers. For instance i want to get 10 random numbers.
from random import randint
x = randint(1, 100)
y = randint(1, 100)
isFailedTest = (5<=x<=15) and (10<=y<=11)
selected_test = [x,y]
while (isFailedTest == False):
I can generate 1 random number at one time but not 10 at one time. Here 1 number mean 2 dimensional number example (x,y) = (10,20) I want to get 10 random numbers (x,y) after my while condition. How do I achieve that? I am very new to programming so could not figure out what could be done. All help/ suggestion/ recommendation is highly appreciated.Thank you.
Simple solution
array = [(randint(1, 100), randint(1, 100)) for i in range(10)]
Better solution
The following solution is more flexible and reusable.
from functools import partial
from random import randint
def randints(count, *randint_args):
ri = partial(randint, *randint_args)
return [(ri(), ri()) for _ in range(count)]
print(randints(10, 1, 100))
Requirement - "Here 1 number mean 2 dimensional number example (x,y) = (10,20) I want to get 10 random numbers (x,y)"
>>> from random import randint as r
>>> array = [ (r(1,100), r(1,100)) for i in xrange(10)]
from random import randint
r = []
N = 10
for x in range(N):
a = randint(5,15) # rand number between 5 and 15
b = randint(10,11) # rand number between 10 and 11
r.append((a,b))
# r <-- contains N tuples with random numbers
Why don't you just do:
from random import randint
randoms = []
for i in range(10):
randoms.append((randint(1,100),randint(1,100)))
Then randoms will be an array of 10 integers, randomly generated between 1 and 100 (inclusive).
To be quite clear: what is going on here is that you make an empty list called randoms. Then the for loop executes ten times, each time appending a new tuple of two random integers to the list randoms.
NumPy should do the job
np.random.randint(low=1, high=100, size=10)
Related
I am trying to generate 4-6 unique numbers in python but when I do uuid.uuuid4(), it generates something like 23dfsFe823FKKS023e343431. Is there a way I can achieve this, generate 4-6 unique numbers like 19391 or 193201.
NB: Beginner with python
yes, to make life easy lets use an easy example.
#lets import random, to generate random stuff
import random
#create a result string
result = ''
nums = [1,2,3,4,5,6,7,8,9,0]
for i in range(6):
result += str(random.choice(nums))
print(result)
UUID is for generating Universally Unique Identifiers, which have a particular structure and won't be what you're after.
You can use the random module as follows
import random
id = ''.join(str(random.randint(0,10)) for x in range(6))
print(id)
What does this do?
randint generates a random number between 0 inclusive and 10 exclusive, i.e. 0-9
calling this with for x in range(6) generates six random digits
str converts the digits to strings
''.join forms a single string from the digits
Try this:
import random
nums = set()
while len(nums) < 4: # change 4 to appropriate number
nums.add(random.randint(0, 1000000))
For example:
>>> nums
set([10928, 906930, 617690, 786206])
You can use random from standard python library
random.randint(a, b)
Return a random integer N such that a <= N <= b.
https://docs.python.org/3/library/random.html#random.randint
In [1]: from random import randint
In [2]: randint(1_000, 999_999)
Out[2]: 587848
In [3]: randint(1_000, 999_999)
Out[3]: 316441
To generate N number of a random number or token by specifying a length of Token this will be an easy and feasible solution.
import random
#num[]: by the combination of this, a random number or token will be generated.
nums = [1,2,3,4,5,6,7,8,9,0]
#all the tokens or random number will be stored in a set called tokens.
#here, we are using a set, because the set will never contain duplicate values.
tokens = set()
result='' #to store temporaray values
while len(tokens) < 10000: # change 10000 to the appropriate number to define the numbers of the random numbers you want to generate.
#change 6 to appropiate number to defined the length of the random number.
for i in range(6):
result+=str(random.choice(nums))
tokens.add(result)
result=''
print(tokens)
#print(len(tokens))
#print(type(tokens))
if you want to use uuid to generate number then you can code something like this
digit = 5 # digits in number
import uuid
for i in range(6):
print(uuid.uuid4().int[:digit])
Or
from uuid import uuid4
result = [ uuid4().int[:5] for i in range(6) ]
I am trying to create a random list of 1 million numbers ranging between 1-100. I have found how to create a single random number but not to create a list of them. In addition, I would prefer to use the numpy uniform function but the solution doesn't have to use this.
For a big array, you'd better use numpy
import numpy as np
X = np.random.randint(1, 101, size=10**6)
Try this simpler one :
from random import randint
my_list = [randint(1, 100) for i in range(1000000)]
The function
np.random.rand(Dim1,Dim2)
will create an array for the given dimensions with values selected from the uniform distribution [0,1]. You can modify this to randomArray = 1 + 99 * np.random.rand(Dim1,Dim2) to have the array with numbers ranging between 1-100.
from random import *
mylist = []
for i in range(0, 100):
f = randint(0, 100)
mylist.insert(i, f)
This code will give 100 random elements to your empty list.
use this as simple as that...don't make your program more complex in code and in efficiency too..
from random import randint
l=[randint(1,100) for i in range(0,1000000)]
I made a random number with this:
import random
number = random.randrange(0,100,2)
I want to iterate in it:
for i in number:
print(i)
or like that:
for number in range (50,100):
print number
I want to make a lists of random numbers, for example 50 random numbers between 0 and 100.
generate a sorted list of 50 different random numbers between 0 and 100 (not included) like this:
sorted(random.sample(range(0,100),50))
(pick 50 elements in the range object and sort them)
If you need/want repeats don't use sample just pick 50 numbers
sorted([random.randrange(0,100) for _ in range(50)])
or from python 3.6: sorted(random.choices(range(100),k=50))
For generating a list with n random numbers, you can use list comprehension. Such as,
lst = [random.randint(0,100) for _ in range(n)]
If you want to make a list of 50 random numbers between 1 and 100, you can do this:
import random
randlist = []
for i in range(50):
randlist += [random.randint(1, 100)]
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)])
I'm writing a ranking algorithm and I would like to add randomness factor .
I'm generating a list of 10 items where each item has a value from 1 to 10 :
l = [random.randint(1,10) for num in range(1, 11)]
I'm running this code around 400 times and storing the large list in a DB .
I would like to add probability to it, so 70% of the times it generate radom numbers between 1 and 5 for the first 5 items, and 5 to 10 for the last 5 items .
And 30% of the times it stays random without any probability (normal mode) .
How can I achieve this using python and it's random module ?
Edit : Some thinks that this might be a duplicate of this question, however it' not, because I'm not looking to choose items based on a probabiltiy, instead I would like to be distributed based on their weight .
The following will generate the wanted random list:
import random
def next_random_list():
# With 70% probability
if random.randint(1, 10) <= 7:
# Generate 5 random elements from 1 to 5
a = [random.randint(1, 5) for _ in range(5)]
# Generate 5 random elements from 5 to 10
b = [random.randint(5, 10) for _ in range(5)]
# Concatenate them
return a + b
else:
# Return just a random list of 10 numbers from 1 to 10
[random.randint(1, 10) for _ in range(10)]
and you can invoke it 400 times:
lists = [next_random_list() for i in range(400)]