Weighted Numbers

Miscellaneous Forums/General Discussion/Weighted Numbers

Anyone know anything about how to make a certain number come up a more than another?

Say i have the numbers 1,2,3, and 4. I want 1 to come up 50% of the time, 2 at 20%, 3 at 15%, and 4 at 10%.

I'm sure I can figure it out but if anyone has already written a system or knows about it right off that be great.

You could generate a random number from 0 to 99, if the number is 0-49, its a 1, if its 50 to 69, its a 2, if its 70 to 84 its a 3, and so on?

Just a thought! :)

Dabz

You could generate a random number from 0 to 99, if the number is 0-49, its a 1, if its 50 to 69, its a 2, if its 70 to 94 its a 3, and so on?

Just a thought! :)
That's pretty much the way I do it.

Ah I thought there was a fancy schmancy way lol. Hmm...lemme write a snippet.


Ah I thought there was a fancy schmancy way lol.



lol, sometimes, the complicated stuff just needs a chin rub and a cup of tea! ;)

Dabz

A very simple way is to fill an array with the numbers, shuffle it and then read through the array in sequence as you need a new number.

In this example 1(50%) 2(20%), 3(10%), 4(10%), 5(10%)

1,1,1,1,1,2,2,3,4,5

I use this method on all my games for deciding what bonuses appear and when. This method is also used extensively on a great many other commercial games.

Snippet from GEOM:
Function Shuffle_PowerObjects()


	Select GameMode
	
		Case SURVIVALMODE
				PowerQueue$ = "5556"
				
		Case ARCADEMODE, ALTERNATIVE
				PowerQueue$ = "222334555666"
				
		Case PRACTICE
				PowerQueue$ = "11122233334"

	End Select

	
	Delete Each PowerObjectQueue
	Local a
	Local length = Len(powerqueue$)
	Dim tempQueue(length)	
		
	For a = 1 To length										; populate Queue
		TempQueue(a) = Int(Mid(PowerQueue,a,1))
	Next
		
	Local temp	
	
	For a = 1 To Length										; Shuffle Queue
		swap = Rand(length)
		temp = TempQueue(a)
		TempQueue(a) = TempQueue(swap)
		TempQueue(swap) = temp
	Next
	
	For a = 1 To length 									; Put queue into Type
		Local poq.PowerObjectQueue = New PowerObjectQueue
		poq\Flavour = TempQueue(a)
	Next
End Function


PowerQueue$ = "5556"
This means that 5 will come up 75% of the time and 6 comes up 25% right?

Yeah.

The trouble with that method is that you'd get the same four-digit sequence over and over - unless you shuffle the array each time you reach the end.

Not sure how fast that would be with a large array?

This would give me 1(50%), 2(20%), 3(20%), and 4(10%).
Like so?
SeedRnd MilliSecs()

Local Que$ = "1111122334"

For a = 0 To 50
	num% = Rand( Len(Que) )
	Print Int(Mid(Que,num,1))
Next



And whats not fancy schmancy about that?

One thing that is disturbing me. When I do this method ten times, the first number follows a sequence even with SeedRnd Millisecs(). Out of 10 tries the first number follows the sequence 1,1,1,1,2,2,3,3,4 then repeats. I'm not sure how thats possible unless...well I just don't know.

It's not repeating for me

indiepath's thing is likely the reason why a lot of games have predictable 'random' events.. I don't know, some older ones could credit bad random number generators with short lists... but Dragon Quest 8 can't claim that. And I ran through one dungeon in and all the way back out 40 or 50 times.. and by the 50th time, I could snap my fingers as the battles started. Oddly enough, the battle I would fight was random, but they all happened at exactly the same times. Since I was running in and out in the same path each time, they all happened in exactly the same spots too.

Unless they forgot to seed the random number generator in dragon quest 8... would seem like a pretty silly mistake for them tho...

@DampeS8N, but when then event occurs you move on to the next one in the sequence, and yes, when you get to the end of the array you re-shuffle.

http://www.blitzbasic.com/codearcs/codearcs.php?code=963

I use Indiepath's method, not that he invented it or anything ;-)

I assume that in DQ8 when you leave the dungeon the array is dumped to make way for all the town variables, when I re-entered it, it started from scratch again, thus making for an identical set of times as the last run through.

There are a lot of reasons to use that method, including the fact that random numbers could be -too- random and it would be easier to control a list of numbers that gets re-organized than it is to control purely random ones. Not to mention speed.

I use Indiepath's method, not that he invented it or anything ;-)
And I borrowed it from Reflexive.

StvieG:
Sswift to the rescue again! :-)

And I borrowed it from Reflexive.
hmm wonder who Reflexive got it from? Let's follow the chain back until we find the originator!

I'm don't think storing the next ten numbers is really necessary for my purposes. I prefer to just get one return at a time.