# Unique Random Integers

For example in my Memorable Ladies games it is the case that I needed a method that gives a random integer for example between 0…31 so that any integer that has once been drawn, doesn’t get drawn anymore.

One way to handle this (how I didn’t do it) could be for example to use Rand-function (depending on programming language one is using) that gives an integer between nm (m > n) and make a list of numbers that are already drawn and use Rand-function again between nm, if the integer given by Rand has already been drawn.

But in the worst case this could lead to infinite loop… In practice probably not, though. In order to avoid the infinite loop (the case where Rand function gives repeatedly a number that has already been drawn), one could for example increase the drawn number by 1 until unused number is found or go on to m and if needed start from n and increase the value by 1 until unused number is found.

In Memorable Ladies games speed isn’t critical factor, when the numbers are drawn, so in this particular case the routine doesn’t necessarily need to be fast. In addition the amount of data can be considered very small.

What I came up, was something where every random number is (in practice) necessarily unique and without possibility to get stuck on infinite loop.

The idea goes like this:

• Let us assume that we need n integers between 0….n – 1

• Make a list of numbers with type ”number” where type ”number” has as a member an integer between 0…n – 1 (the numbers can be in increasing order)

Loop

• Draw an integer between 0…n – 1. Let this be i.

• Get from the list ith ”number” element and get the integer that is in that ”number” element as a member and then delete that element from the list

• Decrease n by 1

• Repeat until list is empty

Even if in the original list of type ”number” the integers are ordered from 0 to n – 1, one would get this way an unique random integer each time (except if Rand gives zero (0) n times).

There are more sophisticated ways to do this, especially if the set of needed integers is large. Image courtesy of Stuart Miles at FreeDigitalPhotos.net

For the sake of nostalgia… Let be mentioned what kind of random number generator I have used with Amiga with MC68040’s built-in FPU in assembly. As such this doesn’t give an unique random number, though.

(In the source code of my old Quest of Love demo you can see this in practice)

Let us assume, that we need an integer between 0…n.

The idea:

• Get the vertical beam value from \$dff006

• ”Scale” the previous value with desired integer in order to get it big enough

• Use FPU’s fsin instruction (sine function) to that value

• Use FPU’s fabs instruction (absolute value) to the value we got in the previous step

Now we have a floating point value between 0…1

• Multiply that value by n

• Convert the value of previous step to integer

That’s it!

If we would like to have an integer between n…m (0 < n < m) the additional steps would be

• Let z = mn

• Add z to the integer we got in the last step earlier

The two steps above are only the idea to get the value to the desired interval; in assembly there are not variables at all as we know them in programming languages of higher level.

This idea probably gives you a good idea how to get random integers with Monkey X’s Rnd() that gives a random value between 0…1. 🙂

Probably the most common way is to do something like following:

value = Rnd() * 100 Mod n

This gives a random integer between 0…n – 1 (0 < n <= 100 above). Notice that in the sentence above there are two possibilities to get zero: 1) Rnd() gives 0, 2) Rnd() gives 1 and n = 100.
{"ft_lang_label":"__label__en","ft_lang_prob":0.9103127,"math_prob":0.9828237,"size":3605,"snap":"2020-24-2020-29","text_gpt3_token_len":865,"char_repetition_ratio":0.13524021,"word_repetition_ratio":0.011661808,"special_character_ratio":0.24327323,"punctuation_ratio":0.052558783,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9928924,"pos_list":[0,1,2],"im_url_duplicate_count":[null,4,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-06-01T08:06:10Z\",\"WARC-Record-ID\":\"<urn:uuid:702c200a-c88b-4595-ad62-fee2d5d4918b>\",\"Content-Length\":\"69458\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:52fc463c-92ea-4050-b43e-e66b4c074cf7>\",\"WARC-Concurrent-To\":\"<urn:uuid:16f84487-2c8c-40da-972a-a7f1ff29450e>\",\"WARC-IP-Address\":\"31.217.193.35\",\"WARC-Target-URI\":\"https://blue-bit-entertainment.com/unique-random-integers/\",\"WARC-Payload-Digest\":\"sha1:ALUPN6R5FK2XTI3LOXPGTO544WJ6OWJ5\",\"WARC-Block-Digest\":\"sha1:3JCMYH2AKKAMVXYBG32DM7LXFNFWEPWZ\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-24/CC-MAIN-2020-24_segments_1590347415315.43_warc_CC-MAIN-20200601071242-20200601101242-00325.warc.gz\"}"}