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; rand() % 99 + 1 will give you 100. If the random number is 99+1, do the math in your ...

  1. #16
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    rand() % 99 + 1 will give you 100. If the random number is 99+1, do the math in your head.
    No the modulus operator produces a remainder, there is no value that you could divide by 99 and add 1 that would produce 100.

    98%99=98+1=99
    99%99=0+1=1

    197%99=98+1=99
    198%99=0+1=1

    etc...

    but

    99%100=99+1=100 //What we want
    100%100=0+1=1
    zen

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

    Zen is right

    Zen is right.

    To get the output 100 from rand()%99+1, rand()%99 must be 99. What rand()%99 do is divide the number generated from rand() with 99, and return the remainder from the division.

    Example:

    5%5=0 (5/5=1, Remainder=0, 1*5=5)
    10%5=0 (10/5=2, Remainder=0, 2*5=10)
    11%5=1 (11/5=2, Remainder=1, 2*5+1=11)
    13%5=3 (13/5=2, Remainder=3, 2*5+3=13)
    29%5=4 (29/5=5, Remainder=4, 5*5+4=29)

    30%5=5 (Not correct, you can divide (subtract) the remainder once more)

    As you can see, the remainder cannot be equal or higher than what you divide with, which makes it impossible for rand()%99 to generate the number 99.
    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.

  3. #18
    Registered User
    Join Date
    Oct 2001
    Posts
    11
    Wow i wanted to know the same question

    but how do u change the random numbers so they are smaller than 100 and bigger than 1

    thanks for any answer
    "Yes, I am a criminal. My crime is that
    of curiosity. My crime is that of judging
    people by what they say and think, not
    what they look like. My crime is that of
    outsmarting you, something that you
    will never forgive me for."
    quote from:
    ..+++The Mentor+++

  4. #19
    Unregistered
    Guest
    random number between 35 and 40 inclusive:

    srand() or randomize() once as per previous discussion:

    x = 35 + (rand() % 6)

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

    Answer

    I'm not sure what you mean, but I'll make a try:

    If you want a random number from 10 to 19, you use this: rand()%10+10.

    General syntax: rand()%Range+Start
    Range is how many different numbers you want (from Start to (Start+Range-1) ).
    Start is the first number (in the above case: 10)

    Example:
    rand()%5+10 A random number from 10 to 14
    rand()%20+80 A random number from 80 to 99
    rand()%100+100 A random number from 100 to 199

    Perhaps you want a random number from all even numbers between 10 and 19 (10, 12, 14, 16 & 18):
    2*(rand()%5)+10

    -Hope you didn't get even more confused
    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.

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

    PS

    PS: I Love to use different colours in the text, as you may have noticed .
    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.

  7. #22
    Registered User
    Join Date
    Oct 2001
    Posts
    11

    Smile

    Thanks Magos =)
    "Yes, I am a criminal. My crime is that
    of curiosity. My crime is that of judging
    people by what they say and think, not
    what they look like. My crime is that of
    outsmarting you, something that you
    will never forgive me for."
    quote from:
    ..+++The Mentor+++

  8. #23
    bobish
    Guest
    You would use rand()%100 + 1
    rand()%100 = 0-99 because it give you the remainder there is no way to get a reamainder of 100 when dividing by 100.

  9. #24
    Registered User
    Join Date
    Sep 2001
    Posts
    25

    thanks i got the rand part working now.....

    to make it into an actually game-like program......
    do the prompts to user..........cin the data.......then use a for loop to count from 10 to 0 (number of try the user gets to guess the number correctly)......

    example
    guess // is number entered by user
    rand_num// is the rand number the program generates
    answer// either 'Y' or 'N' meaning yes or no to play game again.

    for(int i=0;i<=10;i--)
    {
    if (guess!=rand_num)
    {

    cout << "YOu have "<<i<<"tries left.\n";
    }
    else if (guess==rand_num)
    {
    cout<< "Good Work............You got the number in ony " << i << " tries.\n";
    }
    hows that look?

    i was thinking of adding....... this at the end
    }while(answwer=='y'||'Y')
    }

  10. #25
    hehe
    Guest
    Use srand() and rand()%101.

    what is so great about rand()%100+1,

  11. #26
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    The original post asked about a random number generator between 1-100, and rand()%100+1 will do this.

    rand()%101 will produce random numbers between 0-100.
    zen

Page 2 of 2 FirstFirst 12
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, 11:13 AM
  2. rapid random number generation problem
    By Newton in forum C Programming
    Replies: 17
    Last Post: 09-19-2008, 03:08 PM
  3. Generating a random number?
    By Konspiracy in forum C++ Programming
    Replies: 5
    Last Post: 04-28-2007, 01:33 AM
  4. Testing Random Number Generator
    By Roaring_Tiger in forum C Programming
    Replies: 7
    Last Post: 08-12-2005, 01: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, 09:11 PM

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