Create a random number that is unique

D

DDanny

Guest
Hello GM community,

I'm currently developing an application for multiple platforms and I came across this issue. After days and days of rigorously looking on Google and the whole Internet I still can't find a sufficient answer so I decided to post a thread here:

All in all, I want to create a random number that is different from others.
When the app first start, it will create 2 different random numbers and draw them to the screen. Since they are the two first it's quite easy to make them different

Code:
number_1 = 1 + irandom(100);
number_2 = 1 + irandom(100);
while (number_2 == number_1)
    {
    number_2 = 1 + irandom(100);
    }
But there's more. After every 2 seconds I want a new number to be randomly generated, and it should too be different from the rest. Therefore I'm wondering what might be the best way to do this ? I can't just check them one by one because I think that wouldn't be optimized at all (?) I tried using arrays and data lists but I want to know if anyone can think of a better way :)

Sorry of my lengthy presentation. I'm looking forward to everyone's idea and thank you in advance !

Have a nice day all
 

Surgeon_

Symbian Curator
If you want to have a small set of numbers (say 1-100, or 1-1000 or such) you can make a ds_list with every one of those numbers, shuffle it, and then every time you need a new number take the last entry in the list and then delete it (the entry, not the entire list). On the other hand, if you want to work with a wide range of numbers (even maybe the maximal set of 32-bit floats) then your approach with a while loop and comparison would suffice because there are billions of combinations and the chances of the loop stalling would be infinitesimally small.

-Surgeon_
 
D

DDanny

Guest
If you want to have a small set of numbers (say 1-100, or 1-1000 or such) you can make a ds_list with every one of those numbers, shuffle it, and then every time you need a new number take the last entry in the list and then delete it (the entry, not the entire list). On the other hand, if you want to work with a wide range of numbers (even maybe the maximal set of 32-bit floats) then your approach with a while loop and comparison would suffice because there are billions of combinations and the chances of the loop stalling would be infinitesimally small.

-Surgeon_
I tried using a ds_list because I found that I can check if ds_list_find_index return -1. But then I encounter many more problems because I also want some selected numbers to disappear when user clicks on them (so that number can be spawned again), and I struggled to remove them properly. I don't want to go over that but if you're willing to listen I can describe my problem :). Anyway I think I will have a try at the while loop again. Thank you
 

Surgeon_

Symbian Curator
Right now I'm not sure what your problem is but if you can describe it better I'm sure we can work something out*. Lists are in fact very simple to grasp and use.

*Not guaranteed to be today because of my obligations.

-Surgeon_
 
Top