Random Help

Discussion in 'Programming' started by The Bard, Dec 31, 2006.

  1. #1
    I'm making a little program for D&D combat, and I am in need of an random function for making integer...
    #include <cstdlib>
    #include <ctime>
    #include <iostream>
    
    using namespace std;
    
    int RandInt(int a,int b)
    {
         srand(time(0));  
         return a + rand() % (b - a + 1);
    }
    
    int x = RandInt(1,6);
    Code (markup):
    I'm using this function, but the problem is that during one executing it alway displays the same value, in order to change the value i have to re-run the program, and it is not acceptable,so plz help me...
     
    The Bard, Dec 31, 2006 IP
  2. The Bard

    The Bard Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Jesus, does nobody know this... c'mon guys, I really need help...
     
    The Bard, Jan 7, 2007 IP
  3. EvilivE

    EvilivE Peon

    Messages:
    23
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void) {
            int random_number;
    
            srand(time(0));
            random_number = rand();
            printf("%d\n", random_number);
    
            return 0;
    }
    
    Code (markup):
     
    EvilivE, Jan 7, 2007 IP