如何在Fortran中获取一个随机数的数组 [英] How to get an array of random number in Fortran

查看:823
本文介绍了如何在Fortran中获取一个随机数的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码生成一个数组 N 整数随机数,并将结果存入 random_int_array

The code below generates an array of N integer random numbers and stores the result in random_int_array

 N=20
 allocate(array(N/2))
 call random_seed
 call random_number(array)
 random_int_array=int(array*N)

问题是我可能会产生重复在 random_int_array 中,我不想要。如何从这个数组中删除重复数据,或者等价地,如何生成一组唯一的随机数?

The problem is that I might generates duplicates in random_int_array and I don't want that. How can I remove the duplicate from this array or, equivalently, how can I generate a set of unique random numbers?

请注意, array 的维数为N / 2。所以问题基本上是从N中提取N / 2个数字,没有重复。

Note that array has a dimension N/2. So the problem is basically extract N/2 numbers, without duplicates, out of N.

推荐答案

这听起来像你想要的整数从1到19以随机顺序。这将是这些整数的洗牌。参见,例如, http: //tekpool.wordpress.com/2006/10/06/shuffling-shuffle-a-deck-of-cards-knuth-shuffle/ http://en.wikipedia.org/wiki/Fisher-Yates_shuffle

It sounds like you want the integers from 1 to 19 in random order. This would be a shuffle of those integers. See, e.g., http://tekpool.wordpress.com/2006/10/06/shuffling-shuffle-a-deck-of-cards-knuth-shuffle/ or http://en.wikipedia.org/wiki/Fisher-Yates_shuffle

这篇关于如何在Fortran中获取一个随机数的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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