Well, I still have no clue what you want, but the two best ways of doing this in my opinion are:
//Weighted Random
items = [(10,rock), (5,ore), (2,coal), (1,diamond)]
totalOddsRatio = sum(i[0] for i in items)
func GetRandom():
prob = Random() // 0-1 floating point
for i in items:
if prob < i[0]/totalOddsRatio:
return i[1]
else :
prob -= i[0]/totalOddsRatio
This is essentially what ProgramGamer recommended except you don't have to keep track of probabilities, just relative ratios of how much more (or less likely) one item is to another. e.g. rock is 10 times more likely than diamond, ore is 2.5 times more likely than coal, etc.
//Bag without replacement
items = [rock,rock,rock,rock,rock,ore,ore,coal,diamond]
defaultItems = deep_copy(items)
func GetRandom():
if items.IsEmpty():
items = deep_copy(defaultItems)
index = RandomIndex(items.length())
item = items[index]
items.RemoveAt(index)
return item
This has the added benefit that you guarantee that what the absolute worst string can be (i.e. the player is guaranteed to see at least 1 diamond every 9 items). This is what is used in most Tetris games so as to prevent the game from being horrible (i.e. only getting Z pieces).
Excuse the horrible pseudo-code