python生成随机数并求和、判断对错_如何在Python中生成随机数对,包括一个条目相同的对,并排除两个条目相同的对?...

在Python中,想要生成不包含完全相同元素的随机数对,可以使用自定义生成器函数实现。这个函数通过维护一个`seen`集合来跟踪已生成的数对,确保每次生成的新数对都不会与之前生成的相同,即使数对中只有一个元素相同。
摘要由CSDN通过智能技术生成

I'm using Python and was using numpy for this.

I want to generate pairs of random numbers. I want to exclude repetitive outcomes of pairs with both entries being the same number and I want to include pairs which only have one entry being the same number.I tried to use

import numpy

numpy.random.choice(a,(m,n),replace=False)

for it, but it excludes any tupels with the the same entries completely, i.e.

import numpy

numpy.random.choice(a=2,(m=2,n=1),replace=False)

gives me only (1,0) and (0,1) and not (1,1), (0,0), (1,0) and (0,1).

I want to do this because I want to draw a sample of random tuples with a large a and large n(as used above) without getting exactly the same tupels more then once. It also should be more or less efficient. Is there a way that's already implemented to do this?

解决方案

G

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值