Thread: a random number creator--PLZ help !

  1. #1
    email for MystWind avatar MystWind's Avatar
    Join Date
    Feb 2005
    Location
    Holland , The Hague
    Posts
    88

    Question a random number creator--PLZ help !

    hi , i've been trying hard , but it doesn;t work :

    for a combat system ( to do dammage etc ) i need random numbers at first using the rand(); comando works , but it will only give a random value one time and never changes it back.
    also I need to let it be influence by ( for example ) strenght level or so. how can I do this ? I really can't figure it out...

    thanks
    PLay MystWind beta , within two years

  2. #2
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    You need to seed your random number generator once at the start of your program. Subsequent calls to rand() will then give you different values. Look up the srand() function.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  3. #3
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    Quote Originally Posted by MistyWind
    for a combat system ( to do dammage etc ) i need random numbers at first using the rand(); comando works , but it will only give a random value one time and never changes it back.
    first you should know two things about 'rand()' :
    1. its a function not a 'command'.
    2. it generates pseudo-random numbers not random numbers.

    use srand() to initialize the random number generator with a seed number. To get a fairly random seed number , send function time() (defined in ctime , returns the number of seconds since 1970) with the argument NULL.

    this is how it would look like :
    Code:
    ...
    srand(time(0));
    int random_number = rand() % 10 /* makes sure the random number won't be bigger than 10 */
    cout << random_number << endl;
    ...
    you can get different numbers each time now.

    Quote Originally Posted by MistyWind
    also I need to let it be influence by ( for example ) strenght level or so. how can I do this ? I really can't figure it out...
    This can be done in many different ways. Just think for a while , i don't wanna spoil the fun
    Last edited by Brain Cell; 03-05-2005 at 09:16 AM.
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

  4. #4
    email for MystWind avatar MystWind's Avatar
    Join Date
    Feb 2005
    Location
    Holland , The Hague
    Posts
    88

    Thumbs up

    thanks very much brain cell
    Last edited by MystWind; 03-05-2005 at 09:21 AM.
    PLay MystWind beta , within two years

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    We can't do much unless you show us some code.

    Sounds to me like you're calling srand() a lot, if you seem to be getting the same number out of rand()
    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.

  6. #6
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    or he's not calling it at all. Show us your code so we don't start assuming things
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

  7. #7
    email for MystWind avatar MystWind's Avatar
    Join Date
    Feb 2005
    Location
    Holland , The Hague
    Posts
    88

    new one

    ok , I am going to write a new program on random numbers see if i get more troubles . well thank you very much !
    PLay MystWind beta , within two years

  8. #8
    email for MystWind avatar MystWind's Avatar
    Join Date
    Feb 2005
    Location
    Holland , The Hague
    Posts
    88

    Talking wOOt , it works !

    yay this code works ! :

    Code:
    #include <iostream>
    
    using namespace std; // using standard libary //
    
    int main ( int argc , char** argv ) 
    
    { 
        srand(time(0));
    int random_number = rand() % 10 ;// not larger than 10 //
    cout << random_number << endl; //out puts it //
    cin.ignore(); // waits for an input and ignores it //
    }

    thank you 2 very much i'll post it when I've made it being influenced by strenght etc.

    ty !
    Last edited by MystWind; 03-05-2005 at 11:16 AM.
    PLay MystWind beta , within two years

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. rapid random number generation problem
    By Newton in forum C Programming
    Replies: 17
    Last Post: 09-19-2008, 02:08 PM
  2. Random number in range generation.
    By hebali in forum C Programming
    Replies: 19
    Last Post: 03-04-2008, 10:46 AM
  3. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 12:56 PM
  4. random number between negative and positive number
    By anomaly in forum C++ Programming
    Replies: 6
    Last Post: 12-06-2003, 08:40 AM
  5. Random Number Generator
    By Ikurik in forum C++ Programming
    Replies: 16
    Last Post: 08-17-2003, 07:34 PM