public Track chooseTrack(Random random) { Track[] tracks = getTracks(); float[] probs = new float[tracks.length]; float totalProb = 0; for (int i = 0; i < tracks.length; i++) { totalProb += getProbability(tracks[i]); probs[i] = totalProb; // System.out.println(Float.toString(totalProb) + " " + getProbability(tracks[i]) + " " + // tracks[i]); } if (totalProb == 0) return null; while (true) { float rand = Math.abs(random.nextFloat()) * totalProb; // System.out.println("r=" + Float.toString(rand)); for (int i = 0; i < tracks.length; i++) if (rand <= probs[i]) return tracks[i]; } }
/** * Insert all service in services table * * @param noms * @throws SQLException */ public static void insertStations(List<String[]> pdvs) throws SQLException { int idPdv; String nom = ""; String adresse = ""; String ville = ""; String cp = ""; String type_route; String[] ouvert = new String[3]; String[] fermer = new String[3]; Time hor_Ouv = null; hor_Ouv.valueOf("00:00:00"); Time hor_Ferm = null; hor_Ferm.valueOf("00:00:00"); double latitudePdv; double longitudePdv; String path = "src\\DAL\\StationsNOM.csv"; List<String> nomStations = LectureFichier.readTXT(path); Random rand = new Random(); int nbrand = 2; String requete = "INSERT INTO stations " + "(id_Station,Nom,Adresse,Ville,CP,Type_route,Horaire_ouverture,Horaire_fermeture,latitude,longitude) " + "VALUES(?,?,?,?,?,?,?,?,?,?)"; // int[] count= new int[0]; Connection connection = ConnexionManager.GetInstance().GetConnection(); for (String[] pdv : pdvs) { if (!pdv[0].equals("id")) { idPdv = Integer.parseInt(pdv[0]); // integration des noms g�n�r� al�atoirement nbrand = rand.nextInt(nomStations.size() - 2) + 1; nom = nomStations.get(nbrand); adresse = pdv[1]; ville = pdv[2]; cp = pdv[3]; type_route = pdv[4]; ouvert = pdv[5].split(":"); fermer = pdv[6].split(":"); // fermer = pdv[6].replace(':', '-'); hor_Ouv = new Time( Integer.parseInt(ouvert[0]), Integer.parseInt(ouvert[1]), Integer.parseInt(ouvert[2])); hor_Ferm = new Time( Integer.parseInt(fermer[0]), Integer.parseInt(fermer[1]), Integer.parseInt(fermer[2])); latitudePdv = Double.parseDouble(pdv[7]); longitudePdv = Double.parseDouble(pdv[8]); try { PreparedStatement requeteSql = connection.prepareStatement(requete); requeteSql.setInt(1, idPdv); requeteSql.setString(2, nom); requeteSql.setString(3, adresse); requeteSql.setString(4, ville); requeteSql.setString(5, cp); requeteSql.setString(6, type_route); requeteSql.setTime(7, hor_Ouv); requeteSql.setTime(8, hor_Ferm); requeteSql.setDouble(9, latitudePdv); requeteSql.setDouble(10, longitudePdv); requeteSql.addBatch(); int[] count = requeteSql.executeBatch(); // requeteSql.executeBatch(); // System.out.println(count.length); } catch (SQLException sqle) { // TODO Auto-generated catch block ConnexionManager.GetInstance().GetConnection().rollback(); sqle.printStackTrace(); } } } }