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

### » Tue Jan 04, 2011 11:21 am

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
B
1
G
2
Posts: 3
Reputation: 631

### » Tue Jan 04, 2011 1:49 pm

[quote="czarekm":36flqo1t]1. get a unique random number from like 1-10[/quote:36flqo1t]

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

[quote="czarekm":36flqo1t]2. create an array unique radom sort[/quote:36flqo1t]

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!
B
21
S
6
G
10
Posts: 1,024
Reputation: 7,445

### » Tue Jan 04, 2011 3:19 pm

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.
B
1
G
2
Posts: 3
Reputation: 631

### » Tue Jan 04, 2011 3:44 pm

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.
B
161
S
48
G
89
Posts: 7,347
Reputation: 66,249

### » Tue Jan 04, 2011 3:58 pm

[quote="czarekm":241cb2gg]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.[/quote:241cb2gg]

Here's an example cap:

[url:241cb2gg]http://dl.dropbox.com/u/2306601/array_hash_unique.cap[/url:241cb2gg]

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.
B
21
S
6
G
10
Posts: 1,024
Reputation: 7,445

### » Tue Jan 04, 2011 4:06 pm

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
B
23
S
8
G
10
Posts: 1,820
Reputation: 8,242

### » Tue Jan 04, 2011 4:36 pm

Thanks to all of you guys for your fantastic help. Let me first look into the examples and then come back.
B
1
G
2
Posts: 3
Reputation: 631

### » Tue Jan 04, 2011 4:37 pm

[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.[/quote:2s81p5uo]

Actually... nope
Cant seem to add "{", and ",", without turning it into a string.
Curse you ampersand, I thought you were my friend!
B
161
S
48
G
89
Posts: 7,347
Reputation: 66,249

### » Tue Jan 04, 2011 5:13 pm

[quote:3pmvyjxa]Cant seem to add "{", and ",", without turning it into a string.
Curse you ampersand, I thought you were my friend![/quote:3pmvyjxa]

B
9
S
3
G
3
Posts: 366
Reputation: 2,301

### » Tue Jan 04, 2011 5:23 pm

Actually, the @ array access is probably more to blame. I checked with a set pv, and it just wont accept a pv as an array.
Dunno if that's a bug, or just beyond its scope, too bad tho, would make for an interesting way to do some level editing, etc.
B
161
S
48
G
89
Posts: 7,347
Reputation: 66,249

Next