akorx Posted May 19, 2014 Share Posted May 19, 2014 (edited) Hi guys, In one of my scripts I've to generate 5 numbers that are ALL differents. I've tried to do something like that : Do $p = Random(1, 10, 1) $p1 = Random(1, 10, 1) $p2 = Random(1, 10, 1) $p3 = Random(1, 10, 1) $p4 = Random(1, 10, 1) Until ($p <> $p1 <> $p2 <> $p3 <> $p4) But it doesn't work. The only way I've found is to do all the tests like this : Do $p = Random(1, 10, 1) $p1 = Random(1, 10, 1) $p2 = Random(1, 10, 1) $p3 = Random(1, 10, 1) $p4 = Random(1, 10, 1) Until ($p <> $p1 And $p <> $p2 And $p <> $p2 And $p <> $p3 And $p <> $p4 And $p1 <> $p2 And $p1 <> $p3 And $p1 <> $p4 And $p2 <> $p3 And $p2 <> $p4 And $p3 <> $p4) ... imagine if I've to generate 50 numbers between 1 to 1000, the test should be difficult to write ! It's there a best way to do this ??? Edited May 19, 2014 by akorx AkorxMail akorx@yahoo.fr Link to comment Share on other sites More sharing options...
FireFox Posted May 19, 2014 Share Posted May 19, 2014 Hi, There are multiple possibilities using an array. One of them is to fill the array with numbers from 1 to 10 (or other limits) and then scramble it. Br, FireFox. Link to comment Share on other sites More sharing options...
Unc3nZureD Posted May 19, 2014 Share Posted May 19, 2014 Try this one #include <array.au3> Global $RandArray[50] $i = 0 While $i < 50 $num = Random(1, 100, 1) _ArraySearch($RandArray, $num) If @error Then $RandArray[$i] = $num $i += 1 EndIf WEnd _ArrayDisplay($RandArray) Link to comment Share on other sites More sharing options...
mikell Posted May 19, 2014 Share Posted May 19, 2014 #include <Array.au3> Local $p[5], $n For $i = 0 to UBound($p)-1 Do $n = Random(1, 10, 1) Until _ArraySearch($p, $n) = -1 $p[$i] = $n Next _ArrayDisplay($p) Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now