for loop gives the same result

I try to generate randlomly sequenced po, p1, p2, p3(three of them are 0 and the other is 1)But the for loop gives 4 same sequence. Can you help?

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
#include <iostream>
#include <stdlib.h>
#include <time.h>

using namespace std;


int p0, p1, p2, p3;
int main ()
{
	double a0[] = {1,0,0,0};
	double a1[] = {0,1,0,0};
	double a2[] = {0,0,1,0};
	double a3[] = {0,0,0,1};

	for (int i=0; i<4; i++) {
		srand ( time(NULL) ); //initialize the random seed
		const double arrayNum[4] = {0,1,2,3};
		int RandIndex = rand() % 4; //generates a random number between 0 and 3
		if (arrayNum[RandIndex] == 0) {
			p0 = a0[0];  p1 = a0[1]; p2 = a0[2]; p3 = a0[3];
		}
		else if (arrayNum[RandIndex] == 1) {
			p0 = a1[0];  p1 = a1[1]; p2 = a1[2]; p3 = a1[3];
		}
		else if (arrayNum[RandIndex] == 2) {
			p0 = a2[0];  p1 = a2[1]; p2 = a2[2]; p3 = a2[3];
		}
		else if (arrayNum[RandIndex] == 3) {
			p0 = a3[0];  p1 = a3[1]; p2 = a3[2]; p3 = a3[3];
		}

		cout<<p0; cout<<" "; cout<<p1; cout<<" "; cout<<p2; cout<<" "; cout<<p3<<endl;

	}
}
Last edited on
Do not call srand() multiple times. srand() sets the RNG to a particular starting point. Calling srand() repeatedly can cause the RNG to return the same random numbers. srand() should be called ONCE at the beginning of main().
http://www.cplusplus.com/reference/cstdlib/srand/
Try this one:

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

int main ()
{
   vector< vector<double> > a = { {1,0,0,0}, {0,1,0,0}, {0,0,1,0}, {0,0,0,1} };
   srand ( time( 0 ) );     //initialize the random seed ONCE ONLY
   for (int i=0; i<4; i++)
   {
      vector<double> p = a[rand() % 4];
      for ( auto e : p ) cout << e << ' ';
      cout << '\n';
   }
}
Hello grkanklcsln,

Fixing up what you started with and a few changes. Give this a try:
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
#include <iostream>
#include <cstdlib>  // <--- Should use these
#include <ctime>

using namespace std;  // <--- Best not to use.

//int p0, p1, p2, p3;  // <--- Best not to use non constant global variables. Also not necessary as global variables.

int main()
{
    int p0{}, p1{}, p2{}, p3{};  // <--- ALWAYS initialize all your variables.
    double a0[] = { 1,0,0,0 };
    double a1[] = { 0,1,0,0 };
    double a2[] = { 0,0,1,0 };
    double a3[] = { 0,0,0,1 };

    //srand(time(NULL)); //initialize the random seed
    srand(static_cast<unsigned int>(time(nullptr)));  // <--- The more up to date method. Only needs done once.

    for (int i = 0; i < 4; i++)
    {
        const double arrayNum[4] = { 0,1,2,3 };
        int randIndex = rand() % 4; //generates a random number between 0 and 3

        if (arrayNum[randIndex] == 0)
        {
            p0 = a0[0];  p1 = a0[1]; p2 = a0[2]; p3 = a0[3];
        }
        else if (arrayNum[randIndex] == 1)
        {
            p0 = a1[0];  p1 = a1[1]; p2 = a1[2]; p3 = a1[3];
        }
        else if (arrayNum[randIndex] == 2)
        {
            p0 = a2[0];  p1 = a2[1]; p2 = a2[2]; p3 = a2[3];
        }
        else if (arrayNum[randIndex] == 3)
        {
            p0 = a3[0];  p1 = a3[1]; p2 = a3[2]; p3 = a3[3];
        }

        cout << p0; cout << " "; cout << p1; cout << " "; cout << p2; cout << " "; cout << p3 << endl;
    }

    return 0;  // <--- Not required, but makes a good break point for testing.
}


Andy
Hello grkanklcsln,

My apologies. In my hast to head the call of nature I missed some things in your program.

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
#include <iostream>
#include <cstdlib>  // <--- Should use these C++ versions.
#include <ctime>

using namespace std;  // <--- Best not to use.

//int p0, p1, p2, p3;  // <--- Best not to use non constant global variables.

int main()
{
    constexpr unsigned MAXSIZE{ 4 };

    int p0{}, p1{}, p2{}, p3{};  // <--- ALWAYS initialize all your variables.
    int a0[MAXSIZE]{ 1, 0, 0, 0 };
    int a1[MAXSIZE]{ 0, 1, 0, 0 };
    int a2[MAXSIZE]{ 0, 0, 1, 0 };
    int a3[MAXSIZE]{ 0, 0, 0, 1 };

    //srand(time(NULL)); //initialize the random seed
    srand(static_cast<unsigned int>(time(nullptr)));  // <--- The more up to date method. Only needs done once.

    for (int i = 0; i < MAXSIZE; i++)
    {
        constexpr int arrayNum[MAXSIZE]{ 0, 1, 2, 3 };

        int randIndex = rand() % 4; //generates a random number between 0 and 3

        if (arrayNum[randIndex] == 0)
        {
            p0 = a0[0];  
            p1 = a0[1]; 
            p2 = a0[2]; 
            p3 = a0[3];
        }
        else if (arrayNum[randIndex] == 1)
        {
            p0 = a1[0];  p1 = a1[1]; p2 = a1[2]; p3 = a1[3];
        }
        else if (arrayNum[randIndex] == 2)
        {
            p0 = a2[0];  p1 = a2[1]; p2 = a2[2]; p3 = a2[3];
        }
        else if (arrayNum[randIndex] == 3)
        {
            p0 = a3[0];  p1 = a3[1]; p2 = a3[2]; p3 = a3[3];
        }

        cout << p0 << " " << p1 << " " << p2 << " " << p3 << '\n';  // <--- Could also use single quotes here.
    }

    return 0;  // <--- Not required, but makes a good break point for testing.
}

Line 11 makes the program easier to change as you can see where the variable is used.

In lines 13 - 17 the use of "MAXSIZE" is not needed. I put it there as a demonstration.

In line 22 try to avoid using magic numbers as you did. This is one of the more important reasons for using a constant variable.

In lines 13 - 17 and line 24 I changed these to an "int" from the "double". Defining these variable arrays as "double"s then trying to store the "double" in an "int" has possible data loss because you can not store a floating point number into a non floating point variable.

Your if/else if statements are OK, but if you have studied a "switch" it could be an alternate choice.

For line 48 you do not need a separate "cout" for every little part. That is what the insertion operator (<<) is for to chain everything together.

When you define variable the = before the uniform initializer, the {}s, is not needed.

For lines 30 - 33 you may want to consider using this form until you better understand C++ coding an especially the error messages generated by the compiler. At least until you get use to everything.

If the compiler only gives you a line number of the error and there are 4 choices on that line to check, how much time would you waste trying to figure out which statement is the problem.

Last point is to use a good name for your variables. Something more than just a single letter. I makes the code easier to follow and understand.

Andy
Topic archived. No new replies allowed.