创建一个随机位置的矩阵 [英] Create a Matrix with random position

查看:51
本文介绍了创建一个随机位置的矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用这个创建一个矩阵

def letplay(m,n):矩阵 = [[ '.'对于范围内的 m(10)] 对于范围内的 n(10)]对于矩阵中的子列表:s = str(子列表)s = s.replace('[', '|').replace(']', '|').replace(',', "")印刷)

返回

<预><代码>>>>'.'.'.'.'.'.'.'.'.'.''.'.'.'.'.'.'.'.'.'.'......

这会创建一个 10x10 的矩阵.现在我想在矩阵内以概率 = prob 将随机位置添加到随机位置,该概率应由用户输入,并为随机位置返回一个N".

解决方案

如何使用概率矩阵:

probs = numpy.random.random_sample((10, 10))数据 = numpy.empty(probs.shape, dtype='S1')data.fill('.')数据[问题<阈值] = 'N'

其中 threshold 是您选择的变量,例如,0.05 表示 5% 的机会.

I am creating a matrix with this

def letsplay(m,n):
    matrix = [[ '.' for m in range(10)] for n in range(10)]
    for sublist in matrix:
        s = str(sublist)
        s = s.replace('[', '|').replace(']', '|').replace(',', "")
        print(s)

that returns

>>> '.'.'.'.'.'.'.'.'.'.'
    '.'.'.'.'.'.'.'.'.'.'
    ...
    ...

That creats a matrix 10x10. and Now I want to add within the matrix to random position with a probability = prob, the prob should be entered by the user, and return for the random posstions a 'N'.

解决方案

How about using a probability matrix:

probs = numpy.random.random_sample((10, 10))
data = numpy.empty(probs.shape, dtype='S1')
data.fill('.')
data[probs < threshold] = 'N'

where threshold is a variable that you choose, for example, 0.05 for a 5% chance.

这篇关于创建一个随机位置的矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆