http://qs321.pair.com?node_id=388546


in reply to Re: How to generate different random numbers?
in thread How to generate distinct random numbers?

It might be more helpful to return shuffled ints between $start and $end, even if $num < $range. The user just won't get back $num ints. Perhaps the $range+1 returned value could be "Range too small."

As for shuffle vs. rand, "use Benchmark". :-) See if there's a predictable threshold where one overtakes the other.

Update: Is the ratio between $num and $range significant when determining whether a shuffle or rand is better to use? It would seems that a very small $num:$range ratio would benefit from rand, where shuffle-ing is better as $num:$range -> 1.

Or am I speaking from a non-standard orifice?

  • Comment on Re^2: How to generate different random numbers?

Replies are listed 'Best First'.
Re^3: How to generate different random numbers?
by ranjan_jajodia (Monk) on Sep 05, 2004 at 06:26 UTC
    hi johnnywang,
    As soon as you talk about 'distinct', 'random' goes out of the window. So i assume you need distinct numbers upon which you can't apply any pattern. The best way is to write a hash function on the current date + time which ofcourse must not give always increasing or decreasing numbers. Use google to find a suitable algorithm ( i am sure people have done it before).