quick question on random #'s

This is a discussion on quick question on random #'s within the C++ Programming forums, part of the General Programming Boards category; well im not completely sure how this assigns the variable first_die a random number from 1 - 6.. Code: const ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    7

    quick question on random #'s

    well im not completely sure how this assigns the variable first_die a random number from 1 - 6..


    Code:
    const int x = 1;
    const int y = 6;
    int main()
    {
    time_t seconds;
    time(&seconds);
    srand((unsigned int) seconds);
    
    int first_die = rand() % (y - x + 1) + x;
    cout << first_die;
    system("PAUSE");
    return 0;
    }
    I understand that x gets a constant value of 1 and y gets a constant value of 6... but why does the higher value get - from the lower value + 1 and all of it + lower value. I dont know... im a bit confused =P

    o yea, im not really positive with modulus as well....
    Last edited by Tsukasa; 05-06-2005 at 09:11 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,781
    rand() % 6 returns an integer between 0 and 5, inclusive.
    Since you want the range to be 1 to 6 inclusive, you add 1 (i.e. x).
    If you wanted the range to be 2 to 7 inclusive, you add 2.
    But then x would be 2, and y would be 7, yet the size of the range (y - x + 1 = 7 - 2 + 1 = 6) remains the same.
    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

  3. #3
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,807
    Here is some good stuff on random numbers
    http://faq.cprogramming.com/cgi-bin/...&id=1073086407
    Woop?

  4. #4
    Registered User
    Join Date
    Apr 2005
    Posts
    7
    ok, another question...

    Code:
    #include <cstdlib>
    #include <iostream>
    #include <time.h>
    
    using std::cout;
    using std::cin;
    using std::endl;
    
    unsigned const int high = 500000;
    unsigned const int low = 100000;
    int main(){
        time_t seconds;
        time(&seconds);
        srand((unsigned int)seconds);
        
        for (int x;;x++){
            cout << rand() % (high - low + 100000) + low << endl;
            system("PAUSE");
            system("cls");
            }
        return 0;
    }
    can you explained to me srand()...

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    rand does not really generate random numbers. It generates so-called pseudo-random numbers. Those are a really long sequence of numbers, but eventually they repeat.
    The problem is that on each program run, the sequence starts at the same place, so the results of your program would be the same every time. That's why it's possible to seed the generator, i.e. to set it to a specific starting position in the sequence. srand does just that.
    The generator is usually seeded with some timestamp. This ensures that your program, if run at different times, gives different results.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. quick noob question
    By thanatos1 in forum C# Programming
    Replies: 2
    Last Post: 06-17-2009, 08:28 PM
  2. quick question (adding characters)
    By Cactus in forum C Programming
    Replies: 2
    Last Post: 09-24-2005, 03:54 PM
  3. Another brain block... Random Numbers
    By DanFraser in forum C# Programming
    Replies: 2
    Last Post: 01-23-2005, 04:51 PM
  4. very quick question.
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 07-24-2002, 03:48 AM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 05:46 PM

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