开发者

Random numbers in java and creation of tables in mysql

I have the following table

select * from consumer6;

Service_ID | Service_Ty开发者_如何学JAVApe | consumer_feedback |

     93           Computing                            1 
     93           Computing                            1 
     93           Computing                            1 
     93          Computing                             1 
     93          Computing                             1 
     93          Computing                             1 
     93          Computing                             1 
     93          Computing                             1 
     93         Computing                              1 
     93          Computing                             1 
     93          Computing                             1 
     93          Computing                             1 
     93        Computing                               1
     93          Computing                             1 
     93           Computing                            1 
     93          Computing                             1                   
     93          Computing                             1 
     93          Computing                             1 
     93         Computing                              1            
     93       Computing                                1 
     70           Data                                     

select * from consumer5;

Service_ID         Service_Type                consumer_feedback 
     89         Computing                        -1 

I want to create 100 tables for my project.I have Service_Types(data,computing,printer).I am using the random functions

int min1=31;//printer
    int  max1=40;
    int min2=41;//data
    int max2 =70;   
    int min3=71; //computing
    int max3=75;
    int min = 1;
int max = 9;
    int min4=1;
    int max4=30;   //consumer

    int min5 = -1;
    int max5 =1;

   int feedback = (int) (Math.random() * (max5 - min5 + 1) ) + min5;


    int number = (int) (Math.random() * (max - min + 1) ) + min;    

    int provider1 = (int) (Math.random() * (max1 - min1 + 1) ) + min1;

    int provider2 = (int) (Math.random() * (max2 - min2 + 1) ) + min2;
    int provider3 = (int) (Math.random() * (max3 - min3 + 1) ) + min3;
   int consumer=   (int) (Math.random() * (max4 - min4 + 1) ) + min4;

I need the Service types to be distributed in 100 tables such that all the providers (31-40 printer,41-70 data,71-75 Computing to be distributed uniformly with feedback values generating 1 most number of times.Please help me.


public static void main(String[] args) {

    for(int i=0;i<100;i++){
        String table = "table"+i;
        createTableFor(table); //add your table creation info here, but I strongly suggest you to use a single table and add the consumer as a column
        generateRandomData(table,10); 
    }

}

private static void generateRandomData(String table,int sample) {
    Random rnd = new Random();
    int[] feedback = genMostlyPositiveFeedback(sample); //I am assuming you want positive feedback for all, not by service type
    for(int i=0;i<sample;i++){
        int random = rnd.nextInt(75);
        for(Range range:Range.values()){
            if (range.isInRange(random)){
                Object[] data = new Object[]{range,random,feedback[i]};
                System.out.println(Arrays.asList(data)); //replace this with a SQL Insert statement
                break;
            }               
        }
    }       
}

private static int[] genMostlyPositiveFeedback(int sample) {
    boolean mostlyPositive = false;
    Random rnd = new Random();
    int[] feedback = new int[sample];
    while(!mostlyPositive){
        for(int i=0;i<sample;i++){
            feedback[i] = rnd.nextInt(3) - 1; //generates -1, 0 and 1. If you need only -1 and 1, try nextBoolean() 
        }
        mostlyPositive = checkIfItsMostlyPositive(feedback);
    }
    return feedback;
}

private static boolean checkIfItsMostlyPositive(int[] feedback) {
    int countPositives = 0;
    for(int i=0;i<feedback.length;i++){
        if (feedback[i] == 1){
            countPositives++;
        }
    }
    return countPositives > (feedback.length/2 + 1);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜