Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

list of unique random numbers in an Interval in Ada

I'm sorry to bother you I know this question have been asked quite a lot but never with Ada... I was wondering if there were in the Ada standard library a way to generate a list of unique random numbers (you never pick twice the same number) in O(n) In a way is there an implementation of the Knuth-Fisher-Yates algorithm in Ada?

like image 579
charly Avatar asked Mar 19 '11 11:03

charly


People also ask

How do you generate unique random numbers within a range in Excel?

Generating a Set of Unique Random Numbers in Excel In a column, use =RAND() formula to generate a set of random numbers between 0 and 1. Once you have generated the random numbers, convert it into values, so that it won't recalculate again and again to make your workbook slow.

What is unique random number?

This is a pure algorithmic method of generating random but unique numbers without arrays, lists, permutations or heavy CPU load. Latest version allows also to set the range of numbers, For example, if I want unique random numbers in range of 0-1073741821.


2 Answers

There's a discussion of implementing the Fisher–Yates shuffle here. Basically, you need a different range of Discrete_Random in each iteration, as shown here; Float_Random is an alternative, as mentioned in A.5.2(50), Note 16. If bias isn't critical, this example may be sufficient.

In any case, shuffling is O(n), but selecting can be O(1).

Addendum: The complexity of creating the set depends on the implementation. For example, Containers.Hashed_Sets, A.18.8(88/2) and Containers.Ordered_Sets, A.18.9(116/2).

like image 170
trashgod Avatar answered Oct 02 '22 13:10

trashgod


Given that you want: a) Random numbers from 0 to 1000 and b) the numbers are not to repeat according to the link you provided, you could do this rather easily.

Just fill an array with the range of values and perform some number of swaps on randomly chosen elements thereof; this guarantees both requirements are upheld.

like image 41
Shark8 Avatar answered Oct 02 '22 11:10

Shark8