Changing Random Numbers?

This is a discussion on Changing Random Numbers? within the C++ Programming forums, part of the General Programming Boards category; How do I make a function that creates a different random number each time the function is called? Thanks, napKIN...

  1. #1
    napKINfolk.com napkin111's Avatar
    Join Date
    Apr 2002
    Posts
    310

    Question Changing Random Numbers?

    How do I make a function that creates a different random number each time the function is called?

    Thanks,
    napKIN

  2. #2
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753
    first, you need.to.include.<stdlib.h>.for.rand();.Then.put.an ywhere.in.your.program,.....'srand(time(NULL));'.. ...that.will.make.a.new.random.number.each.time.Yo u.must.include.<time.h>.for.that.BTW....I.have.to. use.periods.because.here.at.school.the.space.bar.i s.not.working.now.

  3. #3
    Registered User biosx's Avatar
    Join Date
    Aug 2001
    Posts
    230
    Code:
    #include <iostream>         // cout
    #include <time>             // time()
    #include <cstdlib>          // srand() & rand()
    
    using namespace std;
    
    int main()
    {
       int randNum;
    
       srand( time(0) );      // or srand( time(NULL) )
    
       randNum = rand();
    
       cout << "Your random number is " << randNum << endl;
    
       return 0;
    }
    Each time this program runs it should have a different set of random numbers. The srand( ) function seeds the rand( ) with the time in seconds since 1972 (erh something). Anyway, the srand( ) call is the key to making it generate different sets of numbers each time.

    Good luck.

  4. #4
    napKINfolk.com napkin111's Avatar
    Join Date
    Apr 2002
    Posts
    310
    Thanks alot guys.
    Leeman, I'm at school to....only my space bar works

    napKIN

  5. #5
    napKINfolk.com napkin111's Avatar
    Join Date
    Apr 2002
    Posts
    310
    How can I make it so it creates a random number up to a max. I'm going to use it to deal weapon damage in an RPG. Something like this:

    code:
    ------------------------
    damage=rand(5)+10;
    ------------------------
    End code


    so that the weapon deals from 10-15 damage.

    thanks,

    napKIN

  6. #6
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753
    low + rand() % (high-low+1)

    example for between 1 and 10:

    damage=1+rand()%(10-1+1); //1 through 10

  7. #7
    napKINfolk.com napkin111's Avatar
    Join Date
    Apr 2002
    Posts
    310
    Thanks, but I don't understand a couple things...

    1. I forget what % means(doh!), I'll check that when I get home though
    2. Why "(10-1+1)"? Isn't that the same as 10?


    Thanks Leeman

    napKIN

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, 09:47 AM
  2. Doubts regarding random numbers generation
    By girish1026 in forum C Programming
    Replies: 9
    Last Post: 12-31-2008, 10:47 PM
  3. random numbers
    By mesmer in forum C Programming
    Replies: 4
    Last Post: 10-24-2008, 02:22 PM
  4. Generating a sequence of numbers in a random order
    By mirbogat in forum C Programming
    Replies: 15
    Last Post: 08-12-2008, 03:01 PM
  5. Generating 100k to 1 million unique random numbers
    By Ariod in forum C Programming
    Replies: 4
    Last Post: 08-26-2005, 01:59 PM

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