Thread: Question with rand()

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

    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

    int randomNum(int x,int y)
        int random = rand();
        int number = (random % y)+x;
        return number;
    and then when i call to it i use
    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 i must be using rand() wrong. any help would be awesome! Thanks

  2. #2
    Registered User
    Join Date
    Jan 2005
    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
    The Netherlands
    int random = rand() % rangenumber (+startnumber);

    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
    I really think you should leave out the parentheses around startnumber, Ideswa.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Mar 2006
        int number = (random % (y - x))+x;
    Think a bit and you'll understand why.
    #include <stdio.h>
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    /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
    thanks guys, I was looking over the tutorial on this site for it and it gave the example
    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
    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)
    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!
    STL Util a small headers-only library with various utility functions. Mainly for fun but feedback is welcome.

  8. #8
    Registered User Zero_X's Avatar
    Join Date
    Mar 2006
    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, 09:08 PM
  4. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  5. rand() question
    By abrege in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2002, 08:38 AM