# anyone offering a little help with random number?

• 04-14-2004
TripleF2000X
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!
• 04-14-2004
TripleF2000X
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
• 04-14-2004
Prelude
• 04-14-2004
ggs

advice: look up rand() function, try a while loop.
• 04-14-2004
TripleF2000X
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); }```
• 04-14-2004
TripleF2000X
ignore #include <stdlib.h>
• 04-14-2004
ggs
pseudocode:
Code:

```do {     generate number     display number } while (generatednumber != sentinel)```
• 04-14-2004
TripleF2000X
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.
• 04-14-2004
ggs
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
• 04-14-2004
TripleF2000X
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..
• 04-14-2004
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
• 04-14-2004
TripleF2000X
thank you so much. and i know what you mean. i'm a lil new at this. thanks again for helping me
• 04-14-2004
WaltP
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?!?!?