# Criteria based random numbers

• 09-14-2006
alkisclio
Criteria based random numbers
For my game I need to have a random selection functioinality for weapons. You pickup a box from the ground, and it searches in the weapon array to return a random weapon struct. To get the random struct from the array, we need the index of it, which must be generated randomly. I am using the srand and rand functions to get a random number, but I want to include criteria.

A rocket launcher should have a lower chance of being selected as the random weapon, while a shotgun a higher chance. How would I go implementing this?

Thank you
• 09-14-2006
bumfluff
Work out a few random number ie. 1-3

and if number 1 or 2 (that sort of thing) then weapon = shotgun if weapon = 3 then weapon = rocket launcher.

I hope you understand that.
• 09-14-2006
Salem
Pick a random number between 1 and 100.

If it's <= 50, then it's a hand gun (50%)
if it's <= 75, then it's a shotgun (25%)
if it's <= 85, then it's an automatic (10%)
if it's....
• 09-14-2006
alkisclio
This would work as well, but I was hoping for a solution/algorithm for arbitary ammount of criteria.

If I can't come up with something, I will use this solution. Any other ideas?
• 09-14-2006
Salem
So you use the logic I suggested to fill the box, then select something with equal probablility.
• 09-14-2006
IfYouSaySo
Here's a basic framework for you. Add any number of weapons you like. Make sure you add some code somehwere to do srand()...I assume you already know that part. You could also incorporate some of what Salem said so that certain weapons are more likely than others to come up.

Code:

```# include <vector> # include <iostream> using namespace std; class weapon {     public:     weapon() {}     virtual ~weapon() {}     // other virtual functions here     virtual void use_it() = 0; }; class shotgun : public weapon {     public:     // implement virtual functions here     void use_it() {         cout << "Firing shotgun!" << endl;     } }; class grenade : public weapon {     public:     // implement virtual functions here     void use_it() {         cout << "Throwing grenade" << endl;     } }; // Factory functions to create your objects weapon* create_shotgun() {     return static_cast<weapon *>(new shotgun()); } weapon* create_grenade() {     return static_cast<weapon *>(new grenade()); } // serves out weapons on a random basis class WeaponProvider {     public:     WeaponProvider() {         // TODO: Add srand() code here     }     // register any number of weapons     void register_weapon(weapon*(*create_function)()) {         options_.push_back(create_function);     }     // get a random weapon, don't forget to delete it when you're done     weapon* get_weapon() {         int n = rand() % options_.size();         return options_[n]();     }     private:     vector<weapon*(*)()> options_; }; // demo it int main() {     WeaponProvider wp;     wp.register_weapon(create_shotgun);     wp.register_weapon(create_grenade);     for (int i = 0; i < 10; i++) {       weapon* w = wp.get_weapon();       w->use_it();       delete w;     } }```
• 09-14-2006
alkisclio
Thanks a lot