# anyone offering a little help with random number?

This is a discussion on anyone offering a little help with random number? within the C++ Programming forums, part of the General Programming Boards category; If anyone would be able to help me, i'd really appreciate it please. for school i have a problem to ...

1. ## 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. 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. Post your code.

4. your code thus far please.

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

5. 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. ignore #include <stdlib.h>

7. pseudocode:
Code:
```do {
generate number
display number
} while (generatednumber != sentinel)```

8. 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. 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

10. 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. 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

12. thank you so much. and i know what you mean. i'm a lil new at this. thanks again for helping me

13. 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?!?!?

Popular pages Recent additions