如何获得从1到N的随机数字数组

如何获得从1到N的随机数字数组

本文介绍了如何获得从1到N的随机数字数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码生成一个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

这篇关于如何获得从1到N的随机数字数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 07:52