Thread: Random Number

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    39

    Question Random Number

    I wanna make the program pick a number randomly between 1 and 15. What's the script for that. Also, how do you assign that number to a varible.

    Thanks!
    Compiler: Dev-C++ 4.9.8.0

    -Bert

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Read the faq.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    39
    Now how do ya assign the number to a varible?
    Compiler: Dev-C++ 4.9.8.0

    -Bert

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >Now how do ya assign the number to a varible?
    Using the function name from the faq...
    Code:
    int mynum;
    
    mynum = rand_mid(1, 15)
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Using the time to seed a random number isn't a bad idea.

  6. #6
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Here is one solution.

    -----
    srand(static_cast<unsigned>(time(0)));

    // random number 1 to 15

    int rNum = static_cast<int>((((static_cast<double>(rand())) / (static_cast<double>(RAND_MAX + 1))) * 15 + 1));
    -----

    Kuphryn

  7. #7
    Unregistered
    Guest

    here..

    simple


    PHP Code:
    #include <stdio.h>   //for printf, etc
    #include <time.h>   //for time function in randomize()
    #include <stdlib.h>  //for randoming functions

    main()
    {
            
    int i;
            
    randomize()
           
            for (
    i=1i<=10i++)
            {
                  
    printf("%d\n"1rand()%15 );
            }


    COOL PROGRAMS @ www.akilla.tk

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, 02: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, 12:56 PM
  4. random number tutorial
    By 7stud in forum C++ Programming
    Replies: 3
    Last Post: 07-26-2005, 02:41 PM
  5. Random Number Generator
    By Ikurik in forum C++ Programming
    Replies: 16
    Last Post: 08-17-2003, 07:34 PM