Python随机点名程序代码(python随机点击坐标)

http://www.itjxue.com  2023-03-04 19:43  来源:未知  点击次数: 

Python 生成随机点坐标

import?random

import?numpy?as?np

List?=?np.array([(0,0),(1,1),(1.6,1.8),(3,3)])

d?=?0.5

def?get_random(low,high):

?return((high-low)*random.random()+low)

n?=?0

while?n100000:

?x?=?get_random(0,3)

?y?=?get_random(0,3)

?rand_tuple?=?np.array([x,y])

?tmp_dist?=?np.sqrt(np.sum(np.square(List-rand_tuple),axis?=?1))

?tmp_dist_bool?=?tmp_dist?=?d

?if?np.sum(tmp_dist_bool)?==?len(List):

??print(x,y)

??break

?n?+=?1

if?n==100000:

?print("After",n,"tries,can't?get?a?random?point!Check?whether?the?problem?has?a?solution!")

Python生成随机点

终端调用 python3 test.py 1 1 1 20 ,生成位于圆心为(1,1),半径为1的圆形区域内的20个随机点,得到下图:

python编程 编写程序自动生成0到100间的一个随机数,然后让参与者输入昵称和数字,最后判断谁猜得最准?

#!/usr/bin/python3

# -*- coding:utf-8 -*-

"""

@author:Storm_duck

@file ?:20200605-01.py

@time ?:2020/6/5 15:20

"""

"""

猜数字,看谁猜的最接近

"""

import random

def get_abs(rannum, ansnum):

return abs(ansnum - rannum)

if __name__ == "__main__":

num = random.randint(1, 100)

adic = {}

lens = 0

while True:

choice = input("What's your name?,enter to quit:")

if choice == "enter":

break

if choice != "enter":

answer = int(input("What's your guess(1-100):"))

lens += 1

if choice in adic.keys():

adic[choice] = answer

else:

adic.setdefault(choice, answer)

newlist = sorted(adic.items(), key = lambda kv: get_abs(num, kv[1]), reverse = False)

if newlist[0][1] != newlist[1][1]:

if num == newlist[0][1]:

print("{} 厉害,数字就是{}:".format(newlist[0][0], newlist[0][1]))

else:

print("数字是{},猜的最接近的是:{}".format(num, newlist[0][0]))

else:

temp = []

alist = list(zip(*newlist))[1]

t = alist[0]

for i in range(alist.count(t)):

temp.append(newlist[i][0])

astr = ",".join(temp)

if num == t:

print("{}都比较厉害,数字就是{}:".format(astr, num))

else:

print("数字是{},{}的答案相同,猜的最接近。".format(num, astr))

(责任编辑:IT教学网)

更多