Like Tree1Likes

Can someone please helpe me understand how to use generate random numbers? W/ *rand()

This is a discussion on Can someone please helpe me understand how to use generate random numbers? W/ *rand() within the C Programming forums, part of the General Programming Boards category; I need to generate 3 different random numbers and print out the largest of the 3. The random number needs ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    244

    Can someone please helpe me understand how to use generate random numbers? W/ *rand()

    I need to generate 3 different random numbers and print out the largest of the 3.

    The random number needs to be between 1-100 inclusive.
    My attempt is below....

    1)I don't understand why everytime I run my program it gives the same answer as the MAX? Isn't srand(time(0)); suppose to generate different random numbers everytime?

    2) If i wanted to generate a random number from 40-120 inclusive would I write it like this? rand()%80+40

    3) Unrelated to random numbers, If I wanted to simply print out the time on the computer, how would I do that?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main(void) {
      
      int num1, num2, num3, max;
      srand(time(0));
    
    
    num1=rand()%100+1;
    num2=rand()%100+1;
    num3=rand()%100+1;
    
    if(num1>num2 && num1>num3)
    num1=max;
    else if(num2>num1 && num2>num3)
    num2=max;
    else
    num3=max;
    
    printf("%d", max);
    
    system("PAUSE");
    return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,589
    Quote Originally Posted by matthayzon89
    I don't understand why everytime I run my program it gives the same answer as the MAX?
    Unfortunately, you mixed up the left hand side and right hand side of your assignments, e.g.,
    Code:
    num1=max;
    should be:
    Code:
    max = num1;
    Quote Originally Posted by matthayzon89
    2) If i wanted to generate a random number from 40-120 inclusive would I write it like this? rand()%80+40
    Almost: there are 120-40+1=81 integers in the range [40, 120], hence you should use rand() % 81.

    Quote Originally Posted by matthayzon89
    3) Unrelated to random numbers, If I wanted to simply print out the time on the computer, how would I do that?
    That depends on how exactly you want the time to be represented. Look into the functions from <time.h> for ideas.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    Quote Originally Posted by laserlight View Post

    Almost: there are 120-40+1=81 integers in the range [40, 120], hence you should use rand() % 81.
    How does the computer know to start from 40 ? wouldn't rand()%81 be capable of generating numbers below 40? Also why are you adding 1?

    Maybe it will be easier for me to understand if you can explain how I come up with the first and second number?
    rand()%<**FIRSTNUMBER**>+<**SECOND NUMBER**>

    Thanks for all your help!!
    Last edited by matthayzon89; 04-23-2010 at 11:32 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,589
    Quote Originally Posted by matthayzon89
    How does the computer know to start from 40?
    Sorry, I was just correcting the range, not the start point as that is correct. That is, it should be: rand() % 81 + 40.

    Quote Originally Posted by matthayzon89
    Also why are you adding 1?
    Consider a contiguous sequence of integers: 10, 11, 12. How many numbers are there in the sequence? 12-10=2, or 12-10+1=3?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Apr 2010
    Posts
    10
    You have done a very sill mistake my friend

    you have writtten
    num1=max;

    it should be
    max=num1;

    Same with num2 and num3.

    just note down these mistakes and correct your code and you will get the required output.

  6. #6
    Registered User
    Join Date
    Apr 2010
    Posts
    10
    For generating numbers between 40 and 120 use this

    (rand()%120)+40

    or
    you can use this one also
    random(81)+40

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,589
    Quote Originally Posted by Mr. AB
    For generating numbers between 40 and 120 use this

    (rand()%120)+40
    That is wrong as the range will then be [40, 160).

    Quote Originally Posted by Mr. AB
    you can use this one also
    random(81)+40
    Maybe, maybe not, since this random function is non-standard.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    Can anyone please explain why this code is NOT printing out i=0 and sum=0? (it runs and does not print out anything)

    The while loop is NOT executed b/c the semi colon.
    sum = sum+i; <- is executed independent of the while loop so 0+0=0 <- for sum.

    My logic:
    The printf should print out the 'i=0' from when it was declared and sum=0 from the explanation above.

    My logic#2:
    Does this have anything to do with the boolean expressions are 0 and 1 and 0=FALSE and 1=TRUE and since it values out to 0, then nothing prints out? IF it was any other value besides zero then something would print out? <-- Is that correct?

    Code:
    int i=0, sum=0;
    while (i<10);
      sum = sum+i;
    printf("i=%d,sum=%d\n",i,sum);

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,589
    Quote Originally Posted by matthayzon89
    Can anyone please explain why this code is NOT printing out i=0 and sum=0? (it runs and does not print out anything)

    The while loop is NOT executed b/c the semi colon.
    You have answered your own question

    Here's an exercise: start with the number 0. While 0 is less than 10. Jump.

    Keep jumping.

    Come on, you can do it... jump!

    If you stopped jumping, then you have failed to follow my instructions, because 0 is always less than 10, so you must jump forever

    Quote Originally Posted by matthayzon89
    sum = sum+i; <- is executed independent of the while loop so 0+0=0 <- for sum.
    No, it is never executed, because it is never reached. It can only be reached when the value of i is not less than 10, but the value of i is always 0, and 0 is always less than 10.

    Quote Originally Posted by matthayzon89
    The printf should print out the 'i=0' from when it was declared and sum=0 from the explanation above.
    Likewise, this line is never reached due to the infinite loop.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by laserlight View Post

    Here's an exercise: start with the number 0. While 0 is less than 10. Jump.

    Keep jumping.

    Come on, you can do it... jump!
    Lol laserlight, I love how you combine teaching C with a healthy lifestyle.
    Pinkie22 likes this.

  11. #11
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    Quote Originally Posted by laserlight View Post

    No, it is never executed, because it is never reached. It can only be reached when the value of i is not less than 10, but the value of i is always 0, and 0 is always less than 10.


    Likewise, this line is never reached due to the infinite loop.

    How come the printf and sum=sum+1; are never reached?

    I thought that if there is a while loop with a semi colon after the condition then the loop does not run, and basically everything after it runs normally b/c of that semi colon simply 'stop' the loop b4 it started.

    For example if the while loop was not there it would print out everything normally...right?
    Or if there was a curly brace after sum=sum+i then it would also print...right?

    I know how loops work but my question is more about the semi colons relation ship and how it effects the loop, its very very confusing when it comes to if statements and loops (especially when our prof tries to trick us and purposely does not indent anything, or puts a semi colon in a random place, and we need to predict whats going to happen WITHOUT a computer )

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,589
    Quote Originally Posted by matthayzon89
    I thought that if there is a while loop with a semi colon after the condition then the loop does not run, and basically everything after it runs normally b/c of that semi colon simply 'stop' the loop b4 it started.
    No, that means that the loop has an empty body. But the condition of the loop can still be executed. In this case, you have an infinite loop because the condition of the loop is always true.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    So basically the semi colon has no impact on the code? It executes the while as if the semi colon wasnt there?

    Is this true for all cases (assuming the condition is true)?
    Last edited by matthayzon89; 04-23-2010 at 03:19 PM.

  14. #14
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    That code is not going to print anything because the while will loop for ever but won't do anything because of the ; following it. The 1st printf IS NOT inside the while. Also, if x = 100 for example the while will exit, printf("hello world") would happen and then printf("Boat") will also happen. I'll let you figure out why that is.

  15. #15
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,299
    Quote Originally Posted by matthayzon89 View Post
    So basically the semi colon has no impact on the code? It executes the while as if the semi colon wasnt there?
    No. The semicolon itself constitutes a single statement which becomes the entire loop body. Thus when the loop runs, it does nothing besides check to loop condition over and over.
    If you remove the semicolon then whatever was after that forms the loop body.

    How come the printf and sum=sum+1; are never reached?
    There is no such line in that program. Make sure you use a big enough font to distinguish i from 1.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

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

Similar Threads

  1. Printing random decimal numbers in a text file
    By warrick in forum C Programming
    Replies: 3
    Last Post: 03-08-2010, 08:32 AM
  2. random numbers
    By altf4thc in forum C++ Programming
    Replies: 11
    Last Post: 02-15-2010, 09:03 PM
  3. Help generating multiple random numbers
    By d-dub in forum C++ Programming
    Replies: 7
    Last Post: 10-30-2006, 12:00 PM
  4. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  5. Best way to generate a random double?
    By The V. in forum C Programming
    Replies: 3
    Last Post: 10-16-2001, 04:11 PM

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