Jump to content

This site uses cookies. By continuing to browse the site you are agreeing to our use of cookies. Find out more here. X
X


Photo

Generate Random Numbers without repeating UDF


  • Please log in to reply
2 replies to this topic

#1 billthecreator

billthecreator

    I am The creator

  • Active Members
  • PipPipPipPipPipPip
  • 379 posts

Posted 09 June 2009 - 05:09 AM

I believe this had been done, I just added errors and made the script clear.

the example is in the script, on top, just unblock the comment... you'll see it.
Attached File  GenerateRandomNoRepeat.au3   2.69KB   399 downloads
My Scripts: From Most recent to least.Countdown GUI | QLOCK TWO | FlipClock | Slot Machine My UDF:_GenerateRandomNoRepeat | _GuiSnap







#2 Ashalshaikh

Ashalshaikh

    Wayfarer

  • Active Members
  • Pip
  • 97 posts

Posted 09 June 2009 - 11:36 AM

That's great .....
I will save It
Thank you

#3 WideBoyDixon

WideBoyDixon

    Code Monkey

  • Active Members
  • PipPipPipPipPipPip
  • 381 posts

Posted 09 June 2009 - 12:20 PM

Some observations that I hope you won't mind:

[1] There needs to be a sanity check that you're not asking for something you can't have. For example:
_GenerateRandomNoRepeat(12, 4, 10)

Will presumably never return.

[2] It would be nice to be able to pass a seed through to the function for initialization of the Random function. This would allow the same array to be generated twice using the same seed. Always useful to be able to generate the same "random" sequence twice :D

[3] I'm not convinced the algorithm is as efficient as it could be when, for example, I call it with:
_GenerateRandomNoRepeat(52, 1, 52)

Getting the last number could take a very long time. In those cases, it might be better to create an array containing all the possible numbers 1 to 52, shuffling the array and then returning the first X numbers as appropriate. Obviously this is not an efficient algorithm for:
_GenerateRandomNoRepeat(2, 1, 10000)

So perhaps there could be some logic in order to choose the best way forward.

Regards,

WBD




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users