Random Number!

This is a discussion on Random Number! within the C++ Programming forums, part of the General Programming Boards category; Hey, In a Windows Console Application, how do i output a random number out of certain numbers (e.g. 1 to ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    70

    Random Number!

    Hey,

    In a Windows Console Application, how do i output a random number out of certain numbers (e.g. 1 to 50) And if i want to output another say 5 random numbers how do i make it so the numbers are not the same as the ones outputed earlier??

    So if i wanted to output 5 random numbers, between 1 and 50, and i do not want then to be the same, how do i do it?? is there a function in the standard C++ library for it??

    P.S. I use Microsoft Visual C++ 6

    ciao, and please help.....

  2. #2
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    and let me tell you one mor thing there is no way of generating a true random number from your computer.... The computer uses some algorithm which mimics the generation of true randomness... TO get a true random number you need to get input from your sorroundings such as sound etc etc

  3. #3
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    >there is no way of generating a true random number from your computer

    Not true. How much would you like to bet me that you're mistaken?
    OS: Windows XP
    Compilers: MinGW (Code::Blocks), BCB 5

    BigAngryDog.com

  4. #4
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Originally posted by Davros
    Not true. How much would you like to bet me that you're mistaken?
    There are PCI-cards that generate random numbers using radioactive isotopes.
    But it is impossible to generate true random numbers using only software. Very good random numbers can be generated.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  5. #5
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    >There are PCI-cards that generate random numbers using radioactive isotopes.

    I was waiting Vasanth to bite on that!

  6. #6
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    To generate five numbers between 1 - 50 you can use this:
    for ( int i=0; i<8; i++ )
    {
    cout << 1+rand()%50 << endl;
    }

    Don't forget to include <cstdlib> and if you want to radomize the number generation use the fucntion srand().

    You can use this srand(time(0)), so you the numbers keep changing according to the time the program is being executed.

    Note: To use the function time() you should include <ctime>.
    none...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. rapid random number generation problem
    By Newton in forum C Programming
    Replies: 17
    Last Post: 09-19-2008, 03:08 PM
  2. Random number in range generation.
    By hebali in forum C Programming
    Replies: 19
    Last Post: 03-04-2008, 10:46 AM
  3. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 01:56 PM
  4. random number tutorial
    By 7stud in forum C++ Programming
    Replies: 3
    Last Post: 07-26-2005, 03:41 PM
  5. Random Number Generator
    By Ikurik in forum C++ Programming
    Replies: 16
    Last Post: 08-17-2003, 08:34 PM

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