Random numbers without repeating
PHP Code:
I can use something like: PHP Code:
|
Re: Random numbers without repeating
Don't worry too much about efficiency here.
I ran this code 1 000 000 times with the result of 1.795s. (That's an average of .001795milliseconds per time you randomize 5 numbers... trivial.) Code:
|
Re: Random numbers without repeating
PHP Code:
|
Re: Random numbers without repeating
Thanks you! I already made something that seems is working, but I will use your method, because looks better.
PHP Code:
|
Re: Random numbers without repeating
Quote:
Also @KiLLeR., you can avoid this thing: Quote:
PHP Code:
|
Re: Random numbers without repeating
It depends on implementation.
If it actually is 5 values between 1 and 10 it doesn't matter. But your method of scrambling an array will be more consistent as the previous 2 versions of pure randomizing values will be dependent on the availability of the numbers. As you fill the array and the available numbers become rare, time increases because of retries. Here's a small example comparing the two functions. Code:
Here are some examples I ran: Code:
MIN 1, MAX 10, SIZE 5, RANDOMIZER 10:Code:
MIN: 1, MAX: 10, SIZE: 5, RANDOMIZER: 30Code:
MIN: 1, MAX: 5, SIZE: 5, RANDOMIZER: 10Code:
MIN: 1, MAX: 20, SIZE: 15, RANDOMIZER: 10Code:
MIN: 1, MAX: 20, SIZE: 20, RANDOMIZER: 50Again, this is 1 000 000 runs. So even 41s equals to an average of .041 milliseconds per run. Again, trivial. |
Re: Random numbers without repeating
I know this is already answered but I was bored and put this together.
You must first call PrepRandom() for any min/max combo prior to calling GetRandom() for that min/max combo. Once you call PrepRandom(), you can repeatedly call GetRandom() and you will always get a unique random until all numbers have been used. Once all are used, you will then begin getting the numbers again, and they will be unique until you again use up all the numbers. Like this. But you cannot cannot call GetRandom on a particular min/max until you first call PrepRandom for that min/max set. PHP Code:
PHP Code:
|
| All times are GMT -4. The time now is 18:43. |
Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.