Thread: anyone offering a little help with random number?

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    13

    Exclamation anyone offering a little help with random number?

    If anyone would be able to help me, i'd really appreciate it please. for school i have a problem to do that deals with random numbers. i need to write a program that generates a series of random numbers between 0 and 9 until a zero is generated and then displays the length of the sequence.

    similar outcome to:

    3 2 7 8 8 6 0
    Length was 7.

    if anyone can help me, i'd really like it. thanks!

  2. #2
    Registered User
    Join Date
    Apr 2004
    Posts
    13
    i can get the random numbers, but i can't get it to stop executing when 0 is reached; i'll get an infinite loop of some sort

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Post your code.
    My best code is written with the delete key.

  4. #4
    C > C++ duders ggs's Avatar
    Join Date
    Aug 2001
    Posts
    435
    your code thus far please.

    advice: look up rand() function, try a while loop.
    .sect signature

  5. #5
    Registered User
    Join Date
    Apr 2004
    Posts
    13
    Code:
     
    #include <stdlib.h>
    #include <lvp\random.h>
    {
    	 return rand()%limit;
    }
    
    #include <iostream.h>
    
    int main()
    {
    	const int Sentinel = 0;
    
    	do
    	{
    		randomize();
    		for (int i=1; i<=10; i++)
    			cout << random(10) << " ";
    	}
    	while (lvprandom == Sentinel);
    
    	return(0);
    }

  6. #6
    Registered User
    Join Date
    Apr 2004
    Posts
    13
    ignore #include <stdlib.h>

  7. #7
    C > C++ duders ggs's Avatar
    Join Date
    Aug 2001
    Posts
    435
    pseudocode:
    Code:
    do {
        generate number
        display number
    } while (generatednumber != sentinel)
    .sect signature

  8. #8
    Registered User
    Join Date
    Apr 2004
    Posts
    13
    Code:
     #include <lvp\random.h>
    #include <iostream.h>
    
    int main()
    {
    	const int Sentinel = 0;
    
    	do
    	{
    		randomize();
    		for (int i=1; i<=10; i++)
    			cout << random(10) << " ";
    	}
    	while (random == Sentinel);
    
    	return(0);
    }
    that's a little better, sorry about that.

  9. #9
    C > C++ duders ggs's Avatar
    Join Date
    Aug 2001
    Posts
    435
    that for() loop displays 10 numbers at once (not what you want)

    plus, there is no way of testing whether a generated number is a zero

    try a variable to hold a single generated value at once
    .sect signature

  10. #10
    Registered User
    Join Date
    Apr 2004
    Posts
    13
    for the () loop, i know what you mean; i need it to execute till infinity (technically) until 0 is reached, but i didn't know how to state that in there. and i was confused, i dont know how to name each generated value, i'm not sure we were taught that. that's what we needed to do, i just, well, didn't know how to do it..

  11. #11
    C > C++ duders ggs's Avatar
    Join Date
    Aug 2001
    Posts
    435
    what your current loop does is this:

    do { // start of while loop
    randomize(); // set the random seed
    for(int i = 1; i <= 10; i++) cout << random(10) << " "; // generate and display 10 numbers
    } while(random == sentinel); // loop while random == sentinel

    what you want is something like this:

    do { // start of while loop
    randomize(); // set random seed
    number = random(10); // generate SINGLE number
    cout << number << " "; // display number
    } while(number != sentinel); // continue while NUMBER is not equal to SENTINEL (ie, 0)

    this is not pseudocode, it is pretty much what you need for this assignment. but you've gotta learn to understand the code you're writing, because it will be harder and harder to get a straight answer out of the board w/ questions like this
    .sect signature

  12. #12
    Registered User
    Join Date
    Apr 2004
    Posts
    13
    thank you so much. and i know what you mean. i'm a lil new at this. thanks again for helping me

  13. #13
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Quote Originally Posted by ggs
    what your current loop does is this:

    do { // start of while loop
    randomize(); // set the random seed
    for(int i = 1; i <= 10; i++) cout << random(10) << " "; // generate and display 10 numbers
    } while(random == sentinel); // loop while random == sentinel

    what you want is something like this:

    do { // start of while loop
    randomize(); // set random seed
    number = random(10); // generate SINGLE number
    cout << number << " "; // display number
    } while(number != sentinel); // continue while NUMBER is not equal to SENTINEL (ie, 0)

    this is not pseudocode, it is pretty much what you need for this assignment. but you've gotta learn to understand the code you're writing, because it will be harder and harder to get a straight answer out of the board w/ questions like this
    Except you don't need to set the randomize seed every time you cyle the loop. Move this statement above the loop

    And ggs, no code tags?!?!?
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Random number in range generation.
    By hebali in forum C Programming
    Replies: 19
    Last Post: 03-04-2008, 10:46 AM
  2. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 12:56 PM
  3. How exactly does the random number generator work?
    By Finchie_88 in forum C++ Programming
    Replies: 6
    Last Post: 08-24-2007, 12:46 AM
  4. Random Number Generator
    By Ikurik in forum C++ Programming
    Replies: 16
    Last Post: 08-17-2003, 07:34 PM
  5. Ask about generate Random number
    By ooosawaddee3 in forum C Programming
    Replies: 2
    Last Post: 07-01-2002, 04:30 AM