in reply to Re: Generating 0 .. N Randomly and Efficiently
in thread Generating 0 .. N Randomly and Efficiently

Velaki,
Wouldn't it be easier...

Yes of course, but that's not the point. The point is to learn new things by removing tools (memory) from the toolbox.

Cheers - L~R

  • Comment on Re^2: Generating 0 .. N Randomly and Efficiently

Replies are listed 'Best First'.
Re^3: Generating 0 .. N Randomly and Efficiently
by ikegami (Patriarch) on Oct 19, 2004 at 16:33 UTC

    How does Velaki's solution use more memory (except maybe for (@a), which could be replaced with for ($j=0; $j<@a; $j++))?

    Update: Ah yes, you return an interator instead of building an actual list.

      ikegami,
      As you noted, the list is never constructed - there is only an iterator that finds the next randomly chosen number. Additionally, a bitstring is compact. If you wanted to generate 0 .. 80_000_000, it would create a bitstring only about 1 mb in size.

      Cheers - L~R