how do i make a code that choosea a random number from 1 to 100?

This is a discussion on how do i make a code that choosea a random number from 1 to 100? within the C++ Programming forums, part of the General Programming Boards category; i was messing around an thought that id try to make a simple DOS "Number Guessing " game, i almost ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    25

    how do i make a code that choosea a random number from 1 to 100?

    i was messing around an thought that id try to make a simple DOS "Number Guessing " game, i almost all the code thought up..cept the part to pick the rand0m number?

    anyone know how this can be done?........ if so thanks in advance

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    164
    (rand()%100)+1
    // Gliptic

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Lightbulb Answer

    randomize();

    int Var=random(100)+1; //Variable is set to a number from 1 to 100

    random(n); returns an integer number from 0 to n-1.
    You must include Stdio.h, Stdlib.h or something. can't really remember...
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    electricglidingman
    Guest
    Here's maybe an easier way

    ------------------------
    #include <iostream.h>
    #include <time.h>

    int main () {
    int randomnumber;
    srand (time(0));
    randomnumber = 1+rand() %99;

    cout<<randomnumber<<"That's incredible!/n";

    }

  5. #5
    Quietly Lurking
    Join Date
    Aug 2001
    Posts
    208
    Well since no one seems to have it completly right yet, I guess ill give it a try

    first you will need to include stdlib.h or cstdlib and time.h

    now the first thing your program needs to do is randomize the random number generator so you get new numbers everytime
    so you use the line of code "srand(time(0));" the key is make sure that that line is only run once or your program will crash

    next to get a random number you use rand();
    rand() returns a number between 0 and some large number i think its about 32000, but thats not important.

    so if you use rand()%100 you will get a number between 0 and 99, but you want a number between 1 and 100 so you would want to use rand()%100+1 and that will return a number between 1 and 100

    P.S. Nice avatar Magos, but i belive the character from CT was Magus not Magos
    Proud to be a gun carrying, freedom loving, libertarian
    Don't listen to Right-Wing propaganda, legalize it NOW!

  6. #6
    Registered User Aran's Avatar
    Join Date
    Aug 2001
    Posts
    1,301
    read the faq.

    use code tags.

    thank you.

  7. #7
    Unregistered
    Guest
    Since you are all mistaken, try this.

    #include <iostream.h>
    #include <time.h>

    int main()
    {
    int random_number = 0;

    srand(time(NULL));

    random_number = rand() % 99 + 1;

    cout << random_number << endl;

    return 0;
    }

  8. #8
    Registered User
    Join Date
    Sep 2001
    Posts
    25

    ummmmmmmmm ok whos right :)

    i tried a few all gave at least 2-6 errors

  9. #9
    In The Light
    Join Date
    Oct 2001
    Posts
    598

    uuuuuuum

    i tried unregistered guests version and my borland 5 compiler compiled and linked with one simple error - i changed <<endl; to <<"\n";
    so at least he/she got it right.


    NOW to my question.
    what purpose does (time) serve in a random number generator?

    m.r.

  10. #10
    Registered User
    Join Date
    Sep 2001
    Posts
    164
    This is wrong: rand()%99 + 1
    You will never get 100 with this code

    This is right: rand()%100 + 1


    The time is used as the seed, the original number in the random generator. The number is then scrambled in different ways to get new random numbers.
    // Gliptic

  11. #11
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    The purpose of (time) is to set the seed value of rand() to the current time. This is because the time will always be different so the same number won't come up in the same order every time.

  12. #12
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    To Dalren

    To Dalren:

    Thanks! But I am not Magus, I am Magos (MAGnus OStberg). Though we both look alike, we are not the same persons .


    P.S. Nice avatar Magos, but i belive the character from CT was Magus not Magos
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  13. #13
    Registered User
    Join Date
    Oct 2001
    Posts
    6

    ..

    2-6 errors for each one? Geez what compiler are u using?

    Just check if you're trying to compile these codes in QBasic.

  14. #14
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    try adding

    #include<cstdlib>

    The rand() and srand() funcs. are in that library.
    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

  15. #15
    Unregistered
    Guest
    rand() % 99 + 1 will give you 100. If the random number is 99+1, do the math in your head.

    %100 + 1 will be 101.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  2. rapid random number generation problem
    By Newton in forum C Programming
    Replies: 17
    Last Post: 09-19-2008, 02:08 PM
  3. Generating a random number?
    By Konspiracy in forum C++ Programming
    Replies: 5
    Last Post: 04-28-2007, 12:33 AM
  4. Testing Random Number Generator
    By Roaring_Tiger in forum C Programming
    Replies: 7
    Last Post: 08-12-2005, 12:48 AM
  5. HELP! My rand() won't make a random number...
    By Budgiekarl in forum C Programming
    Replies: 2
    Last Post: 05-18-2003, 08:11 PM

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