Question with rand()

This is a discussion on Question with rand() within the C++ Programming forums, part of the General Programming Boards category; hey guys, im still working on that little turn based game and im trying to add random damage to it. ...

  1. #1
    Registered User Zero_X's Avatar
    Join Date
    Mar 2006
    Posts
    19

    Question with rand()

    hey guys, im still working on that little turn based game and im trying to add random damage to it. but i think im using rand() wrong.

    this is how i am calling it...

    i have this function built to take arguements for the random number

    Code:
    int randomNum(int x,int y)
    {
        srand(time(0));
        int random = rand();
        int number = (random % y)+x;
        return number;
    }
    and then when i call to it i use
    Code:
    int damage = randomNum(20,40);
    in that spot im trying to get a random number between 20-40. but when i run my program i know i am getting a number higher than 40 sometimes...so i must be using rand() wrong. any help would be awesome! Thanks

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    First, you should only call srand() once, at the beginning of your program, not every time you need a random number. Your actual problem is where you put x and y in that code. Follow the calculations by hand and you will see where you went wrong.

  3. #3
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    Code:
    int random = rand() % rangenumber (+startnumber);
    example:

    Code:
    rand() % 20 +20;
    will give 20-40

    Edit: Oops
    Last edited by Ideswa; 04-18-2006 at 02:17 AM.
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,448
    I really think you should leave out the parentheses around startnumber, Ideswa.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Code:
        int number = (random % (y - x))+x;
    Think a bit and you'll understand why.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  6. #6
    Registered User Zero_X's Avatar
    Join Date
    Mar 2006
    Posts
    19
    thanks guys, I was looking over the tutorial on this site for it and it gave the example
    Code:
    random = (rand() % (max - min + 1) + min)
    and im getting all that except why we add +1. my math is not so hot, you guys know why thats needed? or is it?

  7. #7
    Registered User
    Join Date
    Aug 2003
    Posts
    1,198
    Stick some values in there instead and you will see why.

    Say we want to generate random numbers betwean 5 and 10:
    random = (rand() % (10-5) + 5)
    vs.
    random = (rand() % (10-5+1) + 5)

    First one will become this:
    random = (rand() % 5 + 5)
    which will generate numbers betwean (0 to 4) + 5 or 5 to 9
    but we didnt want 5 to 9, we wanted 5 to 10 so...
    random = (rand() % 6 + 5)
    which will generate numbers betwean (0 to 5) + 5 or 5 to 10....YAY!

  8. #8
    Registered User Zero_X's Avatar
    Join Date
    Mar 2006
    Posts
    19
    Ah I see whats going on now, Thanks alot!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  2. Question about the rand() function
    By V4D3R in forum C++ Programming
    Replies: 5
    Last Post: 03-26-2007, 06:37 PM
  3. rand() question
    By Spectrum48k in forum C++ Programming
    Replies: 3
    Last Post: 04-01-2003, 08:08 PM
  4. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 08:22 PM
  5. rand() question
    By abrege in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2002, 07:38 AM

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