Thread: Randomisation problems

  1. #1
    Unregistered
    Guest

    Unhappy Randomisation problems

    I am having trouble telling the computer to select 3 random numbers and write them out.

    Code:
    #include <stdio.h>
    #include <time.h>
    #include <stdlib.h>
    #include <iostream.h>
    #include <windows.h>
    
    int Chance1 , Chance2 , Chance3;
    
    
    void main(void)
    {
    cout << "These 3 numbers should be different: \n";
    {
    srand( (unsigned) time (NULL) );
    Chance1 = rand() % 10;
    cout << Chance1 << "\n";
    
    
    srand( (unsigned) time (NULL) );
    Chance2 = rand() % 10;
    cout << Chance2 << "\n";
    
    srand( (unsigned) time (NULL) );
    Chance3 = rand() % 10;
    cout << Chance3 << "\n";
    }
    }
    There should be three number and each should be different, but some how they all come out the same number!

    Any help would be most appreciated,
    Paul

  2. #2
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    522
    You only need to call srand once in your program
    void main(void)
    this should be:
    which means you need to put a return 0 before the closing brace of main as well
    Code:
    #include <stdio.h>
    #include <time.h>
    #include <stdlib.h>
    #include <iostream.h>
    #include <windows.h>
    
    int Chance1 , Chance2 , Chance3;
    
    
    int main(void)
    {
       cout << "These 3 numbers should be different: \n";
    
       srand( (unsigned) time (NULL) );
       Chance1 = rand() % 10;
       cout << Chance1 << "\n";
    
       Chance2 = rand() % 10;
       cout << Chance2 << "\n";
    
       Chance3 = rand() % 10;
       cout << Chance3 << "\n";
    
       return 0;
    }
    And as your only wanting numbers 0-9 your gonna get the same numbers repeated quite a lot.
    Last edited by C_Coder; 03-03-2002 at 05:40 AM.
    All spelling mistakes, syntatical errors and stupid comments are intentional.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  2. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 05:48 PM
  3. Rendering problems (DirectX?)
    By OnionKnight in forum Tech Board
    Replies: 0
    Last Post: 08-17-2006, 12:17 PM
  4. contest problems on my site
    By DavidP in forum Contests Board
    Replies: 4
    Last Post: 01-10-2004, 09:19 PM
  5. DJGPP problems
    By stormswift in forum C Programming
    Replies: 2
    Last Post: 02-26-2002, 04:35 PM