Reputation: 397
I am generating a random number in my game using:
System.Random rnd = new System.Random ();
int TileType = rnd.Next (0, 8);
The problem is that the numbers are too random. The numbers relate to tiles and i didn't get a particular tile for 20 rolls.
I need guidance on making a function that is generates "fake" random numbers. So basically I would like to make sure i generate each tile type in x rolls. I imagine 12 might be a good starting point for x but would like to be able to adjust x.
If anyone could point me in the right direction it would be greatly appreciated.
Update
Heres what i am currently thinking. Thanks @Golo for pointing me this way. Thoughts?
This should mean that all numbers will come up frequently but you can still have repeats so it will maintain a random feel.
Upvotes: 0
Views: 477
Reputation: 1026
I like @Golo's answer, but just to add a different spin on it . . .
It sounds like his response will only give you each value once. (ie: You will not see value n again until all other values are "rolled" once.)
To keep things truly random while slightly more predictable, try a weighted random number generator.
After each selection, you can 0 out the weight of the selected item (decreasing the chance of it being selected next, and increasing the chance of selecting the "oldest" unselected values.)
These aren't in .Net, but should get you started on the algorithm.
http://www.perlmonks.org/?node_id=242744
http://peterkellyonline.blogspot.com/2012/02/weighted-random-selection-in-php.html
Upvotes: 2
Reputation: 150624
Create a list with the numbers you want to have, and then randomly select an element from this list and remove it.
Repeat this until your list is empty, and start over again.
This way you will get each number once in n
tries, but their order will be random.
Upvotes: 4