Randomisation problems

This is a discussion on Randomisation problems within the C++ Programming forums, part of the General Programming Boards category; I am having trouble telling the computer to select 3 random numbers and write them out. Code: #include <stdio.h> #include ...

  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
    523
    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 04: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, 04: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, 08:19 PM
  5. DJGPP problems
    By stormswift in forum C Programming
    Replies: 2
    Last Post: 02-26-2002, 03:35 PM

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