dimanche 28 juin 2020

c++ random assignment producing issues

I am trying to randomly generate some assignments to a problem that I have other code processing and finding the correct solution for the problem.

The code below is a generate_assignment() function that should randomly assign a value of '0' or '1' where applicable in the "assignment" vectors. Here we are trying to create four threads of assignments. The array of integers "temp_assignment" is just for testing so I can compare between code versions.

But the issue comes when I switch from testing to real time operation using the random assignment generation. In testing, the assignments are done flawlessly and the rest of the code solves the problem as expected. I've tried many different combinations of numbers and it handles it correctly every time. However, when I switch to the random generation, the system only has the correct output about half of the time. Everywhere else in the code where the "assignment" vectors are supposed to be resized and cleared, they are done correctly.

void generate_assignment()
{
    int temp_assignment[10] = {1,1,1,1,1,0,1,0,0,0};

    for(int thread=0;thread<4;thread++){
        for(int i=0;i<literal_count;i++)
        {
            if(unit_literal_assignment[i] == -1)
            {
                //assignment[i] = temp_assignment[i]; //Direct assignment for testing
                if (thread==0){
                    assignment1[i] = temp_assignment[i];
                }else if(thread==1){
                    assignment2[i] = temp_assignment[i];
                }else if(thread==2){
                    assignment3[i] = temp_assignment[i];
                }else if(thread==3){
                    assignment4[i] = temp_assignment[i];
                }
                /*if((rand()%10) < 5) //Actual random assignment
                {
                    if (thread==0){
                        assignment1[i] = 1;
                    }else if(thread==1){
                        assignment2[i] = 1;
                    }else if(thread==2){
                        assignment3[i] = 1;
                    }else if(thread==3){
                        assignment4[i] = 1;
                    }
                }
                else
                {
                    if (thread==0){
                        assignment1[i] = 0;
                    }else if(thread==1){
                        assignment2[i] = 0;
                    }else if(thread==2){
                        assignment3[i] = 0;
                    }else if(thread==3){
                        assignment4[i] = 0;
                    }
                }*/
            }else{
                if (thread==0){
                    assignment1[i] =(int)assignment[i];
                }else if(thread==1){
                    assignment2[i] = (int)assignment[i];
                }else if(thread==2){
                    assignment3[i] = (int)assignment[i];
                }else if(thread==3){
                    assignment4[i] = (int)assignment[i];
                }
            }


        }
    }
    /*for(int i=0;i<assignment.size();i++){
        cout<<assignment1[i];
    } cout<<endl;
    for(int i=0;i<assignment.size();i++){
        cout<<assignment2[i];
    } cout<<endl;
    for(int i=0;i<assignment.size();i++){
        cout<<assignment3[i];
    } cout<<endl;
    for(int i=0;i<assignment.size();i++){
        cout<<assignment4.at(i);
    } cout<<endl;*/


}

Does anyone know what could be the cause of this? Should the random assignment be done in a different way?

I appreciate any comments or help I can get.




Aucun commentaire:

Enregistrer un commentaire