View Single Post
  #1 (permalink)  
Old 07-26-2007, 11:16 PM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
PT Admin
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,119
iTrader: (0)
HelloWorld is a jewel in the roughHelloWorld is a jewel in the roughHelloWorld is a jewel in the rough
Icon13 How does random number generated in C++?

Below is the code that I'm playing around, I'm just trying to figure out on how does the Random number generator works in C++ it's really weird...

PHP Code:
#include "stdafx.h"
#include <iostream>
using std::cout;
using std::cin;

#include <string>
using std::endl;
using std::string;

#include <iomanip>
using std::setw;

#include <cstdlib>
using std::rand;

int main() {
    for (
int i 010i++) {
        
cout << setw(10) << ( rand() % ) << endl;
    }
    return 
0;

More precisely this part:

PHP Code:
rand() % 
I tried to remove the 1, however I think it gives me from 0 to 5, why do we have to do modulus 6? How does it works?

__________________
PHP Code:
System.out.println("Hello World!"); 

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote