random generator

I keep getting the same output and im not sure why since it should be a random value everytime i enter 'r'.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  srand(time(0));
    int x;
    char d;
    while(true){
        
    
    x=rand()%200+1;
    break;
    }
    cin>>d;
    while(d=='r'){
        cout<<x;
        cin>>d;
    }
    return 0;
}
Consider the flow:

1. randomize (good job!)

2. forever loop:
     x
random value in [1,200]
     break loop

3. while user enters 'r' loop:
     print x

Your loop in part 2 is useless -- it never actually loops.
Your loop in part 3 loops as many times as user enters 'r', but you never modify the value of x.

Consider getting rid of 2 and putting the x random value in [1,200] inside loop in part 3.

Hope this helps.
If you include:

1
2
#include <cstdlib>
#include <ctime> 


Here's an example on how you can achieve random numbers between 1-6 (A dice):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <cstdlib>
#include <ctime>
#include <iostream>

using namespace std;

int main(){
	srand(time(0));

	for (int i = 0; i < 25; ++i) {
		cout << 1 + (rand() % 6) << endl;
	}
    return 0;
}

To customize the code for numbers between something. You could edit 1 + (rand() % 6) to suit your purpose.

Basically, what's happening with the code is that the time function is counting from the 1970's or something. Which means that when it's being sent through srand that it changes numbers, making the srand function unique for each time. Thus creating truly "random" numbers that are unpredictable. Then it's sent into an for-loop to be printed 25 times with numbers between 1-6.
Last edited on
The code I have in the beginning is a smaller representation of my actually program. The logic is the same but I actually need to keep the same parts I just need to figure out how to get random values when I press r. I other words I can’t get rid of part 2 and put it in part 3.
Last edited on
I wouldn't doubt it. Here's my take on it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <cstdlib>
#include <ctime>
#include <iostream>

using namespace std;

int main(){
	char d;
        srand(time(0));

	cin >> d;
	while (d == 'r') {

		cout << rand() % 200 + 1 << endl;
		cin >> d;
	}
	return 0;
}


From the code above you will receive random numbers when you press r:
https://i.gyazo.com/0b1ef93ed039546c96ddc22e58ad1ae9.png
Last edited on
LOL, okay. So, rather than think you're going to waste time this way...

@An Integer
Move line 13 out of the loop.
@Duthomhas I guess the first comment was for OP? Also, moved function out of the loop. You are right.
Last edited on
@fivestar - your second loop is just forever outputting x... idk what you expected it to do there. Instead of two, you want a single while loop, basically merging all the logic. This will give x a chance to change at each iteration of the loop (and not stay the same).
its probably confusing without the 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include<iostream>
#include<cstdlib>
using namespace std;

char p0,p1,p2,p3,p4,p5,p6,p7,p8,p9,p10,p11,p12,p13,p14,p15,p16,p17,p18,p19,p20,p21,p22,p23,p24,p25,p26,p27,p28,p29,p30,p31,p32,p33,p34,p35;
char value_checker(char o, char p , char i , int w, int x){
    if(i==o && w==x){
    if(p=='X'){
    p='O';
    }
    else{
    p='X';
    }
    }
    return p;
   }
char value_assigner(){
    char pos;
   char g;
   int e;
   e=rand() % 300+1 ;
   if(e%2==0){
    g='O';
    }
    else if (e%2!=0){
    g='X';
    }
    pos = g;
    return g;
 }
 bool parity_checker(char x0,char x1,char x2,char x3,char x4,char x5){
        int counter=0;
        if(x0=='X'){
        counter=counter+1;
        }
        if(x1=='X'){
        counter=counter+1;
        }
        if(x2=='X'){
        counter=counter+1;
        }
        if(x3=='X'){
        counter=counter+1;
        }
        if(x4=='X'){
        counter=counter+1;
        }
        if(x5=='X'){
        counter=counter+1;
        }
        if(counter%2!=0){
        return true;
        }
        else{
        return false;
        }
}
int main(){
 
char char_input;
srand(time(0));
bool b=true;
do{
while(b){

    
    p0 =value_assigner();
    p1 =value_assigner();
    p2 =value_assigner();
    p3 =value_assigner();
    p4 =value_assigner();
    p5 =value_assigner();
    p6 =value_assigner();
    p7 =value_assigner();
    p8 =value_assigner();
    p9 =value_assigner();
    p10 =value_assigner();
    p11 =value_assigner();
    p12=value_assigner();
    p13=value_assigner();
    p14=value_assigner();
    p15=value_assigner();
    p16=value_assigner();
    p17=value_assigner();
    p18=value_assigner();
    p19=value_assigner();
    p20=value_assigner();
    p21=value_assigner();
    p22=value_assigner();
    p23=value_assigner();
    p24=value_assigner();
    p25=value_assigner();
    p26 =value_assigner();
    p27=value_assigner();
    p28=value_assigner();
    p29=value_assigner();
    p30=value_assigner();
    p31=value_assigner();
    p32=value_assigner();
    p33=value_assigner();
    p34=value_assigner();
    p35=value_assigner();
    
        

      


if((parity_checker(p0,p6,p12,p18,p24,p30) && parity_checker(p0,p1,p2,p3,p4,p5)
    && parity_checker(p6,p7,p8,p9,p10,p11) && parity_checker(p12,p13,p14,p15,p16,p17)
    && parity_checker(p18,p19,p20,p21,p22,p23) && parity_checker(p24,p25,p26,p27,p28,p29)
    && parity_checker(p30,p31,p32,p33,p34,p35) && parity_checker(p1,p7,p13,p19,p25,p31)
    && parity_checker(p2,p8,p14,p20,p26,p32) && parity_checker(p3,p9,p15,p21,p27,p33)
    && parity_checker(p4,p10,p16,p22,p28,p34) && parity_checker(p5,p11,p17,p23,p29,p35))==true){
        b=false;
}
}
cout<< "   1 2 3 4 5 6"<<endl;
cout<< "    ------------"<<endl;
cout<<"A |"<<p0<<" "<< p1<<" "<<p2<<" "<<p3<<" "<<p4<<" "<<p5<<" | "<<"A"<<endl;
cout<<"B |"<<p6<<" "<< p7<<" "<<p8<<" "<<p9<<" "<<p10<<" "<<p11<<" | "<<"B"<<endl;
cout<<"C |"<<p12<<" "<< p13<<" "<<p14<<" "<<p15<<" "<<p16<<" "<<p17<<" | "<<"C"<<endl;
cout<<"D |"<<p18<<" "<< p19<<" "<<p20<<" "<<p21<<" "<<p22<<" "<<p23<<" | "<<"D"<<endl;
cout<<"E |"<<p24<<" "<< p25<<" "<<p26<<" "<<p27<<" "<<p28<<" "<<p29<<" | "<<"E"<<endl;
cout<<"F |"<<p30<<" "<< p31<<" "<<p32<<" "<<p33<<" "<<p34<<" "<<p35<<" | "<<"F"<<endl;



cout<<"Enter 'r' to randomize or position to flip"<<endl;
cin>>char_input; 
}while(char_input=='r');
return 0;
}
Last edited on
Topic archived. No new replies allowed.