Get unique, and random integers

0
Hi guys, I want to make random integers for a list of objects. I found this function: Math.floor(Math.random() * (max-min)) + min; The problem I have is that the integers in the list needs to be unique from the other objects in that list. If I loop over the list of objects, I cannot control that the integer is unique.   Who can help me solve this puzzle?   Thanks, Jacob
asked
3 answers
1

Is my assumption correct that you would do this as a kind of hotfix? Because thenyou can store the numbers you already have used in another list. So after creating check if it is already part of that list and generate a new one if it has already been use.

For creating a new object you could check the database if that number is already in there and otherwise create a new value.

Regards,

Ronald

 

answered
0

If you already have a list with all of your objects. You can simply use a ‘find’ list operation after generating the random number to see if it's already known in the list.

But i’m wondering, does it need to be a random number? If you’re just looking for a unique identifier, you can always use the RandomHash java action in the CommunityCommons to generate a unique ID.

answered
0

Hi Jacob,

Before assigning the generated integer to your object, you could check your database and the list of already created objects (do a ‘find’ on the attribute) to see if its really unique. If yes add to commit list, if not repeat until unique int is generated.

answered