Never-ending Shuffled Sequences - When Random is too Random

Shameless pluggage ahead:
http://kaioa.com/node/53

Note: It’s massive! :slight_smile:

Thanks for the link, it is really insteresting. I will take a closer look to it later.

Nice! That’s pretty elegant.

I’m not so sure I’d want to use randomness like that in all games, though… take diablo, for example. If you knew that killing diablo one hundred times would result in at least one Drop of Special Value™, it’d feel less like a lucky drop, and more like just grinding your way through a deck of cards until you find the one you’re looking for.

In a multiplayer environment you could dodge this issue by using a single global ShuffleBag for this kind of thing.

Well, it’s just another nice tool to have. It certainly isn’t the ultimate solution for any randomness problem. But for those problems it can be used for it’s very handy.

edit: You can of course also multiply the weights eg 2/198 or 10/990 instead of 1/99.

[quote=“oNyx,post:4,topic:31309”]
Absolutely! As I said, you’ve got a very elegant implementation there, solving a problem I had never fully thought about, so I might end up using it in my next game. =)

After reading your blog I came up with this idea in which you’d need double or float probabilities for perhaps extremely low chances of some items drops (like in diablo 2), my idea was that you add items with a percentage probability (0.0-1.0), but if you end up going past 1.0d it would still work.

It isn’t true random but if we go by the premise, true random is not the goal anyway. Only thing is that I am not good with benchmarks, I bet this would be better for huge number of items with extremely low probabilities (as keeping an array spot for each chance would eat memory), but I have no idea which would be better for less extreme cases.

Coded it in notepad bear with that.

ShuffleBag
{
	private java.util.ArrayList< Item > items;
	
	long drawPeriod;
	long drawsLeft;
	
	public class Item
	{
		public Item( String name, double percentage )
		{
			item = name;
			weigth = percentage;
			drawsLeft = 0;
			timesToDraw = 0;
			doneDrawn = true;
		}
		public void setTimesToDraw( long timesToDraw )
		{
			drawsLeft = timesToDraw + this.timesToDraw - drawsLeft; // timesToDraw - ( this.timesToDraw + drawsLeft );
			this.timesToDraw = timesToDraw;
			
			doneDrawn = ( drawsLeft <= 0 );
		}
		public drawn()
		{
			--drawsLeft;
			if( drawsLeft <= 0 ) doneDrawn = true;
		}
		public resetDraws()
		{
			drawsLeft = timesToDraw;
			doneDrawn = ( drawsLeft <= 0 );
		}
		
		String item;
		double weight;
		long drawsLeft;
		long timesToDraw;
		boolean doneDrawn;
	}
	
	addItem( String name, double percentage, boolean pack )
	{
		Item item = new Item( name, percentage );
		
		items.add( item );
		if( pack ) pack();
	}
	pack()
	{
		double totalWeigth = 0;
		double minWeight = Double.MAX_VALUE;
		for( Item i : items )
		{
			totalWeigth += i.getWeight();
			if( minWeight > i.getWeight() ) minWeight = i.getWeight();
		}
		
		double leftoverWeight = 0.0d;
		if( totalWeight < 1.0d )
		{
			leftoverWeight = 1.0d - totalWeight;
			totalWeight = 1.0d;
		};
		
		drawPeriod = 0;
		for( Item i : items )
		{
			long timesToDraw = Math.ceil( i.getWeight() / minWeight );
			i.setTimesToDraw( timesToDraw );
			drawPeriod += timesToDraw;
		}
		
		drawPeriod += Math.ceil( leftoverWeight / minWeight );
		drawsLeft = drawPeriod;
	}
	
	String drawNext()
	{
		boolean reset = false;
		if( drawsLeft <= 0 )
		{
			drawsLeft = drawPeriod;
			reset = true;
		};
		
		long rand = RNG.getLong( 0, drawsLeft );
		
		boolean found = false;
		String s = "";
		long k = 0;
		for( Item i : items )
		{
			if( reset ) i.resetDraws();
			if( found ) continue;
			if( i.isDoneDrawn() ) continue;
			
			k += i.getDrawsLeft();
			if( rand < k )
			{
				s = i.getItem();
				i.drawn();
				--drawsLeft;
				found = true;
				if( !reset ) break;
			}
		}
		
		return s;
	}
}

Hmm thinking about it it would still have the problem where you get several times a low or high random number, perhaps the item list could be shuffled from time to time so the check order changed.