I'm looking for an algorithm, now I'm programming fast, but pseudocode or any similar syntax of the "C family" will do.
Imagine a large list of values, such as pixels in a bitmap. You want to select each visually randomly, one at a time, and you never select the same one twice, and you always end up choosing them all.
I used it before in a fractal generator, so it was not just a line-by-line rendering, but it was slowly creating it in a stochastic way, but that was a long time ago in a Java applet and I no longer have code.
I don’t think he used a pseudo-random number generator, and the main thing that I liked about it was that it didn’t make the rendering time take longer than just a linear approach. Any of the shuffling algorithms that I have looked at will make the rendering take longer with so many values that I need to deal with, unless I miss something.
EDIT: I used the array shuffle method. I will shuffle once when the application loads, and it won’t take so long. Here is the code for my Dealer class.
import Foundation import Cocoa import Quartz class Dealer: NSObject { //######################################################## var deck = [(CGFloat,CGFloat)]() var count = 0 //######################################################## init(_ w:Int, _ h:Int) { super.init() deck.reserveCapacity((w*h)+1) for y in 0...h { for x in 0...w { deck.append((CGFloat(x),CGFloat(y))) } } self.shuffle() } //######################################################## func shuffle() { var j:Int = 0 let total:Int = deck.count-1 for i:Int in 0...total { j = Int(arc4random_uniform(UInt32(total))) deck.swapAt(i, j) } } //######################################################## func deal() -> (CGFloat,CGFloat) { let result = deck[count] let total:Int = deck.count-1 if(count<total) { count=count+1 } else { count=0 } return(result) } //######################################################## }
init is called once and it causes a shuffle, but if you want, you can call shuffle again if necessary. Every time you need a “card”, you name a deal. It ends at the beginning when the deck is executed.