last day (14 days later) » 

5:01 AM
-1
A: How can I never repeat a already generated random number?

Leonardo Savio Dabusvar newNumber:Int = 0 var dealtNumbers:[Int] = [] while dealtNumbers.count < 100 { newNumber = (Int(arc4random_uniform(100)))+1 if !(find(dealtNumbers,newNumber) > -1 ) { dealtNumbers.append(newNumber) } } println(dealtNumbers) or if you prefer to shuffle the array itself yo...

 
It is a condition that limits the array count to 100 numbers, when it reaches it it exits the loop.
yes, you just have to keep the same number inside the function arc4random.
 
@DC39 this is an extremely inefficient method. After you have done 298 numbers you are down to the last two. You might have 3 and 195 left not being used yet. So now you are stuck in a loop generating any of 300 random numbers until you happen to hit either 3 or 195. The more number you have the exponentially slower and slower this will become. My method does not have this disadvantage and works in O(N) time. Which is optimal for this problem.
 
It was not intended to use with 300 numbers, i use it with 52 and it is really fast. I have never tried that with 300 numbers.
Vote down something that works it is ridiculous. I prefer to keep it really random.
 
Even with 52 you should be using another method. With your answer you will need on average 1378 iterations to get all 52 numbers. With mine you need 52. For 300 you need on average 45,000 iterations, with mine you need 300. I'm not down voting because it won't work. I'm down voting because it is incredibly inefficient and there is a much more efficient way to do it.
My answer is equally random. Yours just takes a lot longer to generate all numbers.
 
Sorry man but I am already using it without any lost in performance. It never took more than 1 sec to do finish it and it did 169x last time I've tested it with 52 combinations, 529x for 100 combinations, 1350 for 300 combinations.
 
5:01 AM
My numbers may be off. It's 2:30 in the morning and I'm on my iPad. However, 1,350 is over 4 times 300. Hence, you have just proven that your method is over four times slower than my method. Thanks :-)
 
0.55s thats what it takes here at playground with 52 numbers
 
@LeonardoSavioDabus Here you go, playing around a bit with XCTest... dropbox.com/s/yhyen67m2g55wfv/… For 1000 numbers your method takes 1.041 seconds. My method takes 0.020 seconds. That makes mine 520 times quicker for 1000 numbers generated in a random order. Like I said, your may way work. But it is ridiculously slow.
 
I have updated the answer with your suggestion.
 
@LeonardoSavioDabus just testing for 10,000 numbers. Mine took 0.204 seconds. Yours is still going. I'll update when it finishes.
 
First of all, It is not your method, second it is a shuffle, not randomly generated, it is randomly swapped and i think it will never start twice in a row with the same number. You have to generate the numbers also before shuffling. Again I use it to generate 52 numbers which is working fine on all machines and/or devices.
 
5:01 AM
"Your method", "My method", I don't think either can claim ownership. That's why I called it the "Fisher-Yates Shuffle" as (IIRC) that is the name of the people who first created such a shuffle. It has been adapted and modernised since then but it keeps their name. As for my use of "your" and "mine" I am merely referring to the answer that you posted vs the answer that I posted (which is fairly obvious really). As for "your" method on 10,000 numbers... it's still going. I'm just trying to point out the importance of getting the right algorithm and knowing the complexity of what you're doing.
You also seem to have taken the updated answer from here... stackoverflow.com/questions/24026510/… Going to keep this running overnight now :) I'm interested to see how long it takes.
 
I have given the credits to the author as you can see on my answer source: Nate Cook. I've also mentioned that this method was suggested by you.
 
@LeonardoSavioDabus it just finished as I was about to go to bed. So, 10,000 numbers. Fisher-Yates shuffle: 0.204 seconds. Saving previously generated random numbers: 1343.883 seconds (22 minutes ish). That's 6587 times longer that it took to run. For a relatively small set of numbers.
 
Who cares? I will never use it with more than 52 numbers and I've already showed the random swap option for him also.
 
My point is that you shouldn't be promoting the use of such inefficient algorithms. Especially not on a site like StackOverlow where (as an accepted answer it will spread into the wide world) and even more so in your own code. The fact that I have just shown you how slow it is (even for 52 numbers it was 3 or 4 times slower) you should now be diving into your own code and changing it. My point was that above all you need to understand the number space you are working in and the complexity of what you are doing. You are deliberately using a very inefficient algorithm here and now it's accepted.
You have no idea who will read this accepted answer and what they will be using it for. Saying that you only use it for 52 numbers is a moot point.
 
I have heard your point and I've already updated the answer. What else are you expecting from me? Have a good night.
 
5:01 AM
Nothing, I only commented because you asked "who cares? What is your point?" (Which you then edited). Tbh, the correct thing to do would be to only show the fisher Yates shuffle and delete the original answer.
 
I have already mentioned that it is slower.
 
Why mention it at all?
 
Why don't you mention also that the results you get with your method don't look random at all. You reversed the order of the array before starting and now most of the first elements of the resulting array are high numbers really close to 300.
 
@LeonardoSavioDabus I don't mention that because that's complete rubbish. I guess you down voted mine. Oh well, some people will never learn. I reversed it because I was trying to iterate it backwards. It is still random. It may have higher numbers at the beginning if you run it once. Then again, it may not. Lol. Are you really trying to defend "your" method? Look, keep using your really slow code. I really don't care. Just don't try to tell other people to use it. It is wrong, that's why I down voted it.
@LeonardoSavioDabus making up stuff about other peoples code doesn't make yours any more correct. Lol!
 
Mine is good enough for the purpose I use.
 
5:01 AM
@LeonardoSavioDabus lol, yup, I thougt so. You're still trying to defend code that even you have shown is very inefficient. I've shown that even for small numbers it is thousands of times slower. You still didn't explain your claim about my code being incorrect though. Lol.
 
Have a good night.
 
I will. Would love you to remove the down vote though. The reason you have put it on there is not correct. Unlike my down vote which is correct (as I have shown). :-)
 
If you change your code and show a really random result at the first run I will definitely.
 
@LeonardoSavioDabus it is random. Try running it more than once. If you actually understood the code you took from someone else's answer then you would see that they are doing the same thing.
 

last day (14 days later) »