Thread: Could someone help me with randnum()?

  1. #16
    Banned
    Join Date
    Aug 2017
    Posts
    861
    use brackets in your if statement to keep two things together, your printout of all 1's and another HoldcountPlaceKeeper

    Code:
    int howmanytimes = 0;
    
    loop start
    
    if ( something is true)
    {
        printf(" yep\n");
       howmanytimes++;
    }
    
    end loop
    
    printf(" how many times it was true is %d\n, howmanytimes);
    I'd think by now you should at least understand if statements with and without brackets.
    Last edited by userxbw; 10-15-2017 at 05:07 PM.

  2. #17
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by csmith03 View Post
    I'm not sure what that means I tried that code and it just printed "t = 1, element is 1". I need the print statement to print "The number of times the integer one occurs from your random numbers is %d"... I just don't know how to associate the array with the 100 random integers on the interval [0,1] and find how many times 1 occurs.
    post #13

  3. #18
    Banned
    Join Date
    Aug 2017
    Posts
    861
    you need to slow down and think about what you are doing instaed of just trying to get the quick answers to your problems, as it is not helping you.
    Code:
    int randNum;
    int myRandomNumberArray[100] = { 0 }; 
    int t = 99, g = 0   ;
    srand((int)time(0)); // Seeds Random Numbers
     
     
      // what is this one doing?
     
        while(t >= 0) // Will iterate the conditions until counter hits 0.
        {
            randNum = rand()%2;     // Finds random numbers on interval [0,1]
            printf("\n Random Number is %d", randNum); // Will display Randon Number found.
            t = t-1; // Counter for while condition
        }
         
    // what is this one doing?
    
        for(t=99; t>0; t--)
        {
            myRandomNumberArray[g++] = (rand()%2);
            printf("\nNumber one occurs %d times\n", myRandomNumberArray[g++]);
     
     
        }
    that is not even correctly executed.
    1. declare data types needed
    2. fill your data types with the data needed
    3. process data by whatever means needed.
    this one requires that you find out certain data by its value, and keep track of how many times that took place.then print that out.

    you have an array of size 100 that first needs to get filled with a number between 1 - 2. 100 times not 99

    then you need to read your data and find the 1's, out of 100 then or while you're doing that you have to keep a count of how may times that occurred then print it out to the screen.

    where is the best place / time to keep count of its occurrence?
    where is the best time / place to have it print out the total count?
    Last edited by userxbw; 10-15-2017 at 05:19 PM.

  4. #19
    Registered User
    Join Date
    Oct 2017
    Posts
    39
    Userxbw, thank you. I figured out how to get the print statement to print how many ones there are, but I used too many int variables. I'm only allowed to use TWO int variables and ONE int array.

    I feel like I'm so close, but to meet the specifications I'm going to have to completely destroy this code, because I'm not allowed the three int variables, need to replace one with an int array, store the values into the array, then print them in ascending order.

    Here's current code:

    Code:
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    #include <time.h>
    
    
    int main(void)
    {
    int count = 1;
    int randNum;
    int t = 99;
    srand((int)time(0)); // Seeds Random Numbers
    
    
    
    
    	while(t > 0) // Will iterate the conditions until counter hits 0.
    	{
    		randNum = rand()%2;		// Finds random numbers on interval [0,1]
    		printf("\n Random Number is %d", randNum); // Will display Randon Number found.
    		t = t-1; // Counter for while condition
    		if(randNum == 1)
    		{
    			printf(" Yes");
    			count++;
    		}
    		else if(randNum == 0)
    		{
    			printf(" No");
    		}
    		
    	}
    	
    	printf("\n The number one appears %d times \n", count);
    	
    	
    	
    	
    }

  5. #20
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by csmith03 View Post
    Userxbw, thank you. I figured out how to get the print statement to print how many ones there are, but I used too many int variables. I'm only allowed to use TWO int variables and ONE int array.

    I feel like I'm so close, but to meet the specifications I'm going to have to completely destroy this code, because I'm not allowed the three int variables, need to replace one with an int array, store the values into the array, then print them in ascending order.

    Here's current code:

    Code:
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    #include <time.h>
    
    
    int main(void)
    {
    int count = 1;
    int randNum;
    int t = 99;
    srand((int)time(0)); // Seeds Random Numbers
    
    
    
    
        while(t > 0) // Will iterate the conditions until counter hits 0.
        {
            randNum = rand()%2;        // Finds random numbers on interval [0,1]
            printf("\n Random Number is %d", randNum); // Will display Randon Number found.
            t = t-1; // Counter for while condition
            if(randNum == 1)
            {
                printf(" Yes");
                count++;
            }
            else if(randNum == 0)
            {
                printf(" No");
            }
            
        }
        
        printf("\n The number one appears %d times \n", count);
        
        
        
        
    }
    that is plenty, remember scope, and reuse of the same int variables is valid

    Code:
    int a = 0;
    
    for ( a = 0; a < 100; a++)
    do_something
    
    
    for ( a = 100; a > 0 ; a--)
    do_something
    same var used twice, still got one left over. right? Be creative.

    that / this last code you're not using your array in a for loop to fill it 100 times then use it in another for or while loop to find the 1's and keep count. ( hint) and you do not need this
    Code:
    srand((int)time(0)); // Seeds Random Numbers
    Code:
    rand() % 2;
    is enough
    Last edited by userxbw; 10-15-2017 at 05:46 PM.

  6. #21
    Banned
    Join Date
    Aug 2017
    Posts
    861
    another point you need to practice your condistions.

    Code:
    int a;
    for ( a = 99; a > 0; a--)
     printf('a = %d\n",a);
    
    //or  same thing:
    
    for ( a = 0; a < 99; a++)
    printf("a = %d\n", a);
    
    
    int a = 99;
    // Will iterate the conditions until counter hits 0. ( your words)
     while(a > 0) {
      
    printf("a = %d\n", a);
    a--;
     }

    you're trying to get 100 out of 99 with a zero based array 0 ... 99 = 100.but what does the evaluations of these really do?
    < and > and <= and >= and see exactly where they stop in a loop.

  7. #22
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by userxbw View Post
    and you do not need this
    Code:
    srand((int)time(0)); // Seeds Random Numbers
    Code:
    rand() % 2;
    is enough
    That's true for testing, but it is not true in general: it is indeed necessary to call srand to seed the standard pseudorandom number generator if one wishes to simulate the effect of generating random numbers on each run of the program.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #23
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by laserlight View Post
    That's true for testing, but it is not true in general: it is indeed necessary to call srand to seed the standard pseudorandom number generator if one wishes to simulate the effect of generating random numbers on each run of the program.
    not going to argue but I am taking into consideration as well that this has a fix amount of numbers, being only two, 0 or 1, which doesn't leave much room to play. seems a little redundant to me.
    Last edited by userxbw; 10-16-2017 at 05:48 AM.

  9. #24
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by userxbw
    not going to argue but I am taking into consideration as well that this has a fix amount of numbers, being only two, 0 or 1, which doesn't leave much room to play. seems a little redundant to me.
    The PRNG behind srand and rand, whatever the implementation, has a finite range to begin with. You're confusing the range of the PRNG with the notion of simulating randomness. If you don't explicitly seed the PRNG, it will be as if it were seeded with srand(1), hence the result of running the PRNG will be the same on each run of the program. This is great for having consistent input during testing, but is typically bad when you actually want to run the program in "production".

    If you don't believe me, compile this program once and run it three times, waiting for some time between each run:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
        int i;
        for (i = 0; i < 10; ++i)
        {
            printf("%d ", rand() % 2);
        }
        putchar('\n');
        return 0;
    }
    Then compile this program once and run it three times, waiting for some time between each run:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main(void)
    {
        int i;
        srand((int)time(0));
        for (i = 0; i < 10; ++i)
        {
            printf("%d ", rand() % 2);
        }
        putchar('\n');
        return 0;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #25
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by laserlight View Post
    The PRNG behind srand and rand, whatever the implementation, has a finite range to begin with. You're confusing the range of the PRNG with the notion of simulating randomness. If you don't explicitly seed the PRNG, it will be as if it were seeded with srand(1), hence the result of running the PRNG will be the same on each run of the program. This is great for having consistent input during testing, but is typically bad when you actually want to run the program in "production".

    If you don't believe me, compile this program once and run it three times, waiting for some time between each run:
    Never said I do not believe you, that is why I didn't argure, I just gave you my logic behind it. You're the college girl, not me ( I'm a boy ) . You're the edecate programmer brain in this, I am just a brain trying to figure it out with scholasticism.

    but I am still going to run that program you took your time to write for me to see what I see. thanks!

    agree != argue :
    Last edited by userxbw; 10-16-2017 at 07:02 AM.

Popular pages Recent additions subscribe to a feed

Tags for this Thread