Random Number Loop,

I have almost completed what I think is what I want from the program, except one of the loops first prints out a strange number and the rest of that loop seems to print fine. The problem loop seems to be the 'i' loop, but its first run returns the value 135991312. The program is supposed to print a list of random numbers. The first number is from 1-100. The second is 1-105, third from 1-110 etc. When a number returned is 101 or more, the loop ends. Because I am fairly new at C++ I haven't properly used functions, loops etc. properly so let me know if I can simplify the code :P

Any ideas?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <conio.h>
#include <stdio.h>
#include <windows.h>
#include <time.h>
/* Author hotchoc26 */

int main()
{
system("cls");
srand(time(0));
printf("Welcome! Press any key to begin.");
getchar();	

int iii, ii, i, num=2, pos, dropPos=0, wkPos, wk_Pos, w;

for (ii = 1; ii == 1; ii++)
{
printf("\nWeek 1: %1d", 1 + (rand() % 100));

		if (ii % 5 == 0) 
		{
			printf("\n");
		} 
} 

printf("\n");

for (i = 0; i <= 100; i++)
	{	
	printf("\nWeek %d: %1d", num, pos);
	i = 1 + (rand() % 110);
		if (i % 1 == 0) 
		{
			printf("\n");
			num = num+1;				
		}
	pos = i;
	}


for (dropPos = 0; dropPos <= 0; dropPos++) 
	{

		printf("\nThe song dropped out at position %1d", 101 + (rand() % 100));

		if (dropPos % 1 == 0) 
		{
			printf("\n");
		} 
	} 
	
return 0;
}	
Last edited on
Seems you didn't initialize pos when you declared it, so the compiler is outputting the garabage stored in that location. Happens to all of us. :D It works after because you then assign it the value of i in the first iteration of the loop.
for (ii = 1; ii == 1; ii++)
This loop will execute exactly once, so there's no point to having the loop. You could just set ii=1 and include the code within the loop.

Lines 32 & 46: Since 1 evenly divides all numbers, X%1 is always zero so you don't need the "if" statements.

The program is supposed to print a list of random numbers. The first number is from 1-100. The second is 1-105, third from 1-110 etc.
When a number returned is 101 or more, the loop ends.

I don't see code that does either of these things.
Thanks for the replies. Keene, I'm not sure what you meant about the declaration of pos. Did you mean I had to assign it a value? Or do I have to put some of the for loop code before the loop so the initial run of the loop knows what value to return the first time? I've assigned =0 to pos in the declaration but it gives 0 so it must be something else...

Have I amended the if statements correctly?

I've tried to change the code to do what I said it was supposed to do. I don't know how to find out if the max random number increases every time the loop runs or not. It's supposed to be random from 1-100, then 1-115, 1-120 etc. making it less likely for the program to be able to return a random number from 1-100. Here is the amended code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <conio.h>
#include <stdio.h>
#include <windows.h>
#include <time.h>
/* Author hotchoc26*/

int main()
{
system("cls");
srand(time(0));
printf("Welcome! Press any key to begin.");
getchar();	

int iii, ii, i, num=2, pos=i, dropPos=0, wkPos, wk_Pos, w;

ii = 1; ii == 1; ii++;
{
printf("\nWeek 1: %1d", 1 + (rand() % 100));

		if (ii % 1 == 0) 
		{
			printf("\n");
		} 
} 


for (i = 0; i <= 100; i++)
	{	
	printf("\nWeek %d: %1d", num, pos);
	i = 1 + (rand() % 110);
i % 1 == 0; 

	printf("\n");
	num = num+1;				
	
	pos = i;
	i = i + 5;
	}


for (dropPos = 0; dropPos <= 0; dropPos++) 
	{

		printf("\nThe song dropped out at position %1d", 101 + (rand() % 100));

dropPos % 1 == 0;
printf("\n");
	
return 0;
	}
}	


If you know how to sort my problems out, is there any way to have just one loop to work out all 'weeks' including week 1 and the 'dropout' position? I originally wanted the main loop to show the last random number (the one that surpasses 100) as well as stop the loop. Do I need an array for that?
I think I've got a firmer grasp on what you're trying to do. You want the program to continually loop until the song's position falls out of the top 100. This means there is only one condition where you would stop looping which means you can achieve this with one loop, so psuedocode would look like such:

-Loop while the song is still in the top 100
-Reassign the song's position each iteration, increasing the range of the random number by five each time.
-Output the week's ranking each iteration

You could accomplish this all in one loop with only three variables all declared within the scope of the for-loop (like so):

 
for (int a = 0, b = 0, c = 0; /* logic */; /* increments */) {}
So I've completely changed the code to just one loop. I haven't completely understood what you meant, but a few common sense ajustments and I think it is mostly working as it is supposed to, thank you! A few more questions:

How do I get the program to print the last number which stops the loop? The number that is over 100? You know in the first code revision I had three loops, the third to show a random number separate from the other loop, but I could put this loop in again if it is difficult to show it this way. Do I need an array? And would I need an array to compare all results of the loop? For example, put the value of the first 'week' in a printf, or print the smallest number from that loop, or print the biggest difference between any two consecutive numbers? I really suck at arrays so I hope I don't have to use them :(

Here's the amended code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <conio.h>
#include <stdio.h>
#include <windows.h>
#include <time.h>
/* Author hotchoc26 */

int main()
{
system("cls");
srand(time(0));
printf("Welcome! Press any key to begin.");
getchar();	

for (int a = (1 + (rand() % 100)), b = 100, c = 1; a <=100; b+5) 
{
printf("\nWeek %d: %1d", c, a);
a = 1 + (rand() % b);
a % 1 == 0;
b = b+5; 
c = c+1;				
}
return 0;
}	
Last edited on
Much better so far! I can see you're getting closer to what you want.

A couple of things right off the bat. I used a, b, and c as an example, but you should use much more descriptive variable names to make code easier to understand. So "a" in this case should be something like song_rank. "b" should be rank_modulus (or something like that). "c" should be week.

You do not need an array. Your for loop will quit on the first instance when song_rank > 100, which is the value you will want to output. You can declare song_rank immediately outside the for loop, and then output its final value after it like so:
1
2
3
4
5
6
7
8
int song_rank;

for (int week = 1, rank_modulus = 100; song_rank <= 100; week++, rank_modulus += 5)
{
     /* Update song_rank and output */
}

printf("The song dropped out at %i.\n", song_rank);


Other things about your code: you haven't utilized your for-loop correctly. You didn't use the correct increment operator in your for-loop's declaration. Instead of b+5 use b += 5. Also, keep all your increments in the for-loops declaration and keep them out of the body of the loop.
What was I supposed to do to the loop? I didn't understand what you meant to do the 101+ last number output. Do I replace the loop I have?
Last edited on
I've got the dropout number to show properly but I don't think I've done what you said to:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <conio.h>
#include <stdio.h>
#include <windows.h>
#include <time.h>
/* Author hotchoc26 */

int main()
{
system("cls");
srand(time(0));
printf("Welcome! Press any key to begin.");
getchar();

int pos;

for (int pos = (1 + (rand() % 100)), max = 100, week = 1; pos <=100; max+=5, week++) 
{
printf("\nWeek %d: %1d", week, pos);
pos = 1 + (rand() % max);
pos % 1 == 0;				
}

printf("\nThe song dropped out at %i.\n");
return 0;
}	


Is it possible to retrieve statistics from the numbers for the 'weeks'? Can I get, for example, the:
First number,
Lowest number of all numbers,
Biggest difference between any two consecutive numbers where the second is smaller/bigger than the first, from First number to Last number, which also shows these numbers,
Total number of numbers returned in the 1-100 range, 1-40 and 1-10 ranges,
Total number of occurances where two consecutive numbers are identical, where a value 100 is returned, or if three in a row return 100 * 2, four is *3 etc.,
Total number of occurances showing the value '1'. If true print 50, two occurances is 150, three is 300, 1,000, 1,500, 3,000, 10,000. If 7 or more, print 10,000.

Hopefully an easy 'total score' which adds up the numbers!

Here's basically how the game works I am trying to make a program of:

[url]http://www.ukmix.org/forums/viewtopic.php?f=21&t=104927[/url]
Last edited on
The for-loop is near perfect. Just a couple of things. Here's what it should look like:
1
2
3
4
5
6
7
8
9
10
int pos = 0;

for (int max = 100, week = 1; pos <=100; max+=5, week++) 
{
    pos = 1 + (rand() % max);
    printf("\nWeek %d: %1d", week, pos);
}

printf("\nThe song dropped out at %i.\n", pos);
return 0;


(1) You have to remove the declaration of pos on line 16 of your code. It messes up the scope of the variable in 14. It is legally correct, but I think you are intended to modify the variable on line 14 inside the for-loop not the one declared on line 16.

(2) I initialized the variable declared on line 14 to 0. This allows pos <= 100 to evaluate true, and you're going to be changing the value of the pos variable in the loop anyway, so its initial value isn't all that important.

(3) I switched lines 18 and 19. This way it goes: mod 100 -> output -> mod 105 -> output. The way you had it before was mod 100-> output -> mod 100 -> output -> mod 105 -> output. In short, it lagged behind a week.

As for other things you want to do, you can do all of that. Utilizing arrays and functions is your best bed in that capacity. Arrays allow you to do all that stuff after the fact instead of convoluting your for-loop, and functions mean you don't have to have 10-15 variables to hold all those stats.

(4) Fixed printf function call on line 23.
Thanks! Do I need to replace the loop with an array, or put functions within the array, or all three of them? And is there a place on the forum for a tutorial on arrays/functions?

I tried running the .exe without the program but it dissapears when the numbers are printed...
Last edited on
I don't want to sound impatient but... :D
Read http://www.cplusplus.com/forum/beginner/1988/ to know how to prevent your console from closing. The easiest way would be to run the program directly through the command line, or using an IDE that has the option to keep the program up.
Thanks!
Topic archived. No new replies allowed.