Random acces to matrix using seed in Java -
how can access matrix (int[][] matrix) using random (with seed)?
i want print matrix randomly.
how use seed , random methods? can give me example?
seed is important. want write once.
int[][] matrix = ini(); // print matrix randomly using seed...?
set seed of random instance
random rand = new random(seed); you can inclusive random value calling nextint method...
int row = rand.nextint(matrix.length); ...and use these values access matrix:
int val = matrix[row][otherrandomvalue]; to print matrix randomly, need loop on length of row , column. avoid duplicate values, can keep set of indexes (such using java.awt.point class) , check set see if value has been used yet
set<point> uniques = new hashset<point>(); ( int = 0; < matrix.length; i++ ){//loop on rows ( int j = 0; j < matrix[i].length; j++ ){ point p = new point(rand.nextint(matrix.length), rand.nextint(matrix[i].length)); while ( uniques.contains(p) ){ p = new point(rand.nextint(matrix.length), rand.nextint(matrix[i].length)); } uniques.add(p); //p.x , p.y contain indexes } }
Comments
Post a Comment