Python generates non-repeating random numbers and solves the problem of ordering the list

不言
Release: 2018-04-09 15:05:57
Original
2054 people have browsed it

Below I will share with you a method for generating non-repeating random numbers in python and reordering the list. It has a good reference value and I hope it will be helpful to everyone.

andom.sample(list, n) randomly selects n different elements from the list

# -*- coding: utf-8 -*- 
import random 
# 从一个list中随机挑选5个 
list = [12, 23, 13, 14, 78, 234, 123, 12345] 
randomlist = random.sample(list, 5) 
print randomlist 
 
# 在range(10)中随机生成5个不重复的数,可以作为随机下标集合,然后到list中取数 
len = list.__len__() 
indexList = range(len) 
randomIndex = random.sample(indexList, 5) 
for i in randomIndex: 
 print "下标为%d" % i 
 print list[i]
Copy after login

shuffles the list , shuffle sorting, random.shuffle(list), note that the original list will be changed

# -*- coding: utf-8 -*- 
import random 
# 对list洗牌,在原list上做改变 
list = range(10) 
print list 
random.shuffle(list) 
print "随机排序列表 : ", list
Copy after login

I have been interviewed about generating random non-repeating numbers. Twice, one of the application scenarios was lottery.

The solution I proposed at that time was: put the generated random number (random subscript of the array) in an array, and each time the newly generated random number first determines whether it already exists, and if it does not exist, Add this array, and if it exists, regenerate random numbers until the number of elements in this array reaches a specific value, and then take this random subscript array to the original array to get elements. The interviewer asked me what the time complexity was, and I said O(n^2). The interviewer asked me if I had any improvement plans, but I thought about it for a while and couldn't figure it out.

After returning to the dormitory, my roommate said, You can exchange the selected element and the last element each time. The next time you generate a random number, it will be among the first n-1 elements. Generate, so that you only need to exchange elements once each time, and you don't have to go to the array to determine whether the current subscript has been generated once. The time complexity becomes O(n), which is admirable.

Java provides the list.contains(ele) function, which can directly determine whether an element exists in the specified container. This eliminates the need to write a double loop, but the time complexity is still It’s O(n^2)

, but today I looked at the sample function in python (randomly selecting seed points) and it can directly achieve the results I want. Next time I will write a lottery function and it will be done with just one line of code.

Related recommendations:

How to randomly select elements from a list in python

The above is the detailed content of Python generates non-repeating random numbers and solves the problem of ordering the list. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!