Thread: randomize how to use?

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    1

    Angry randomize how to use?

    i m want to learn c++
    i have a problem
    i dont know how to use rand()
    can u help me ?

  2. #2
    Unregistered
    Guest
    randomize() is one of the functions that will seed a random number generator, like rand(). You should use randomize() just once in your program, but you may use rand() as often as you wish. To generate a random integer you would do this:

    int main()
    randomize();
    long x
    x = rand();
    cout << x;
    return 0;

    Of course you will need to include all appropriate headers.

  3. #3
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    There are 2 approaches to this.....

    1) read the damn frequently asked questions....

    2) the functions you need are srand() to seed the random number generator and rand() to give you a random number between 0 and RAND_MAX.You will find these functions in <cstdlib> or <stdlib.h>

    so...
    Code:
    #include<iostream>
    #include<cstdlib>
    #include<ctime>
    using namespace std;
    
    int main()
    {
    // seed the random num gen. with system time for unique randomness
    srand((unsigned)time(NULL));
    // now a loop
    for(int i=0;i<10;i++)
    {
    //get a random number between 1 and 100.
    //we use modulus to get a number in the desired range.
    //+1 because %100 returns 0-99
    int randomnum=rand() %100 +1;
    cout<<randomnum<<endl;
    }
    return 0;
    }
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. randomize array blackjack c program
    By bazzano in forum C Programming
    Replies: 4
    Last Post: 09-01-2005, 02:05 PM
  2. don't want to randomize two alike numbers
    By randomizer in forum C++ Programming
    Replies: 8
    Last Post: 05-26-2005, 07:42 PM
  3. randomize hex digits
    By wazilian in forum C Programming
    Replies: 3
    Last Post: 12-14-2002, 03:20 AM
  4. Problem with Visual C++ ( randomize function )!
    By marCplusplus in forum C++ Programming
    Replies: 2
    Last Post: 12-17-2001, 01:01 PM
  5. Randomize Number Function
    By beyonddc in forum C Programming
    Replies: 3
    Last Post: 12-10-2001, 04:31 AM