View Single Post
  #4 (permalink)  
Old 07-25-2007, 10:55 PM
rpgfan3233 rpgfan3233 is offline
PT Staff
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 4
Join Date: Jul 2007
Posts: 118
iTrader: (0)
rpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura about
I'm not sure how it is done using .NET (VS2005 is .NET), but in normal C++, it can be done the following way:
Code:
// iostream - IO functions
// cstdlib - stdlib.h (C header); used for srand and rand functions
// ctime - time.h (C header); used for time function as argument to srand

#include <iostream>
#include <cstdlib>
#include <ctime>

//constants for the maximum/minimum values of the generated number
//  only change the MAX_VAL and MIN_VAL. everything else should be fine.
#define    MAX_VAL    6
#define MIN_VAL    1
#define    RANGE    (MAX_VAL - MIN_VAL + 1)

using std::cout;
using std::endl;

int main () {
    // seed the random number generator with current time as unsigned value
    srand((unsigned)time(0));

    //a simple bit of math for a random value between two integers
    // (you can find others if you want a better one
    //   as this is not exactly random enough sometimes)
    cout << ((rand() % RANGE) + MIN_VAL) << endl;

    return 0;
}

__________________
"C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off."
-- Bjarne Stroustrup, creator of what is now known as C++
For more quotes by Bjarne Stroustrup, check out http://www.research.att.com/~bs/bs_faq.html#really-say-that.
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
The Following User Says Thank You to rpgfan3233 For This Useful Post:
HelloWorld (07-25-2007)