Criteria based random numbers

This is a discussion on Criteria based random numbers within the C++ Programming forums, part of the General Programming Boards category; For my game I need to have a random selection functioinality for weapons. You pickup a box from the ground, ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    3

    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

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    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.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,497
    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....
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    3
    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?

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,497
    So you use the logic I suggested to fill the box, then select something with equal probablility.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    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;
        }
    
    }
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    3
    Thanks a lot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. Doubts regarding random numbers generation
    By girish1026 in forum C Programming
    Replies: 9
    Last Post: 12-31-2008, 09:47 PM
  3. random numbers
    By h_howee in forum C++ Programming
    Replies: 3
    Last Post: 12-21-2005, 01:56 PM
  4. random numbers limit
    By HAssan in forum C Programming
    Replies: 9
    Last Post: 12-06-2005, 06:51 PM
  5. random numbers
    By lil_plukyduck in forum C++ Programming
    Replies: 5
    Last Post: 01-14-2003, 09:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21