0 Favourites

# Unique random or unique random array sort (a fiver offer)

This forum is currently in read-only mode.
• 10 posts
• Total Newbie:

I will give a fiver for anyone to tell me how I can

1. get a unique random number from like 1-10

2. create an array unique radom sort

Develop games in your browser. Powerful, performant & highly capable.

Construct 3 users don't see these ads
• 1. get a unique random number from like 1-10

Random(10)+1 will return a number from 1 to 10.

2. create an array unique radom sort

Do you mean a random value for each cell of the array that's unique too? Maybe it's me, but I can't quite tell what you mean.

You can loop through each element of the array and set the current cell to random(x). If you don't want to add numbers to the array that already exist in the array (since you said "unique"), things get a little more complicated.

I sometimes use a method where I take advantage of the "Check if key exists" condition of the Hash table object in that context, but it would be tedious to explain. Maybe better suited for an example.

Still maybe you could clarify first what you actually wanted. And welcome to the forums btw!

Yes, I need the unique numbers in the array, like numbers from 1-10 in different order each time an event triggers it, like a left mouse button click, to make it simple.

• If you just need an array of 10 random numbers, in a range of 1 to 10, the list box will store them for you nicely, and it will even sort them for you, alphanumerically, if you like.

+Start of layout

+repeat 10 times

There are quite a few data structures you can use tho...

Hell you could even use a private variable now.

{6,8,2,8,4,2,4,8,2,9}

That coupled with array access would be fairly elegant.

• Yes, I need the unique numbers in the array, like numbers from 1-10 in different order each time an event triggers it, like a left mouse button click, to make it simple.

Here's an example cap:

http://dl.dropbox.com/u/2306601/array_hash_unique.cap

The X and Y size of the array control the number range. Initially it's set to 10*8, which will result in 80 array cells holding numbers from 1-80 randomly. If you want a 1-10 range, set the Array size to 5*2 for example. I also included a visual way to show the array when you run it, so you can see that it works. Clicking left randomizes it again.

• PixelRebirth was a few minutes faster than me. Anyway, here is another .cap doing basically the same, but without a hash table

random array.cap

• Thanks to all of you guys for your fantastic help. Let me first look into the examples and then come back.

• [quote:2s81p5uo]Hell you could even use a private variable now.

{6,8,2,8,4,2,4,8,2,9}

That coupled with array access would be fairly elegant.

Actually... nope

Cant seem to add "{", and ",", without turning it into a string.

Curse you ampersand, I thought you were my friend!

• [quote:3pmvyjxa]Cant seem to add "{", and ",", without turning it into a string.

Curse you ampersand, I thought you were my friend!