在java中生成一个包含0和1的矩阵,同时选择概率
我有以下代码使用 java.util.Random 和二维数组生成 0 和 1 的矩阵。我想知道是否有一种方法可以定义矩阵中每行出现 0 和 1 的数量的概率。我希望生成 0 和 1 的概率相等。
我想将这个用于生成矩阵的 matlab 等效代码转换为 java 等效代码。
G= rand(10,20)<.5
有什么想法或建议吗?
谢谢, 巴维亚
I have the foll0wing code to generate a matrix of 0's and 1's using java.util.Random and a 2d array. I was wondering if there is a way to define the probability of the number of 0's and 1's occurring per row in the matrix. I want to have an equal probability of generating both 0's and 1's.
I want to convert this matlab equivalent code to generate a matrix into the java equivalent.
G= rand(10,20)<.5
Any ideas or suggestions?
Thanks,
Bhavya
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
没有预定义的方法可以用数字填充数组,但您需要遍历每个元素并为其分配数字。像这样的事情:
这假设默认的随机生成器足够好。如果您希望 1 或 0 的概率更高,请调整 0.5。
There is no predefined method for filling your array with numbers, but instead you need to do lop over each element and assign it the number. Something like this:
This assumes that the default random generator is good enough. Adjust the 0.5 if you want higher probability for 1s or 0s.
随机并不是完全随机,但是,对于此用例,如果您只是执行以下操作,则不太可能注意到差异:
之后,您可以创建一个频率表来查看运行的样本的实际分布。
Random is not perfectly random, however, for this use case you are very unlikely to notice the difference if you just :
Afterwards you can create a frequency table to see the actual distribution over however many samples you run.