本文介绍了如何从整数数组中选择一个随机数,并使其每次都是唯一的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我编写了一个代码来生成遗传算法的群体,其中每个染色体是一个大小为5 * 5的矩阵,其中每个细胞与通过调用方法calculateCellsCenters(200,325)生成的坐标相关联。对于每个染色体,我需要为名为AUV的对象生成随机位置,其中应该从该方法生成的位置中选择该随机位置。我需要AUV的位置对于每个染色体是唯一的,但不幸的是我得到了重复,我认为所选择的位置不是从整个可用选项中选择的,而是仅从一个部分中选择的。任何想法?? 使用系统; 使用 System.IO; 使用 System.Text; 使用 System.Drawing; 使用 System.Collections; 使用 System.ComponentModel; 使用 System.Windows.Forms; 使用 System.Data; 使用 System.Threading; 使用 AUV_Topology; 使用 System.Collections.Generic; 使用 System.Media; 使用 System.Linq; static int [,] auvChromosomes; static int [] [,]人口; static int [,] auvChromosomeLocations; static int randomIndex; // 从cellsCenters数组中选择随机X或Y坐标,用于每个染色体中的AUV随机情况 static int prevRandomIndex; // 确保每次我们获得不同的randomIndex; static int [,] cellsCenters; static int [] columnsXs; static int [] rowsYs; private void moveGenetic_Click(对象 sender,EventArgs e) { FileStream fs = new FileStream( C:/Users/Welcome/Desktop/intialPopulation.txt,FileMode.Append,FileAccess.Write); population = new int [ 6 ] [,]; // 锯齿状数组 auvChromosomeLocations = new int [ 6 , 2 ];对于将要生成的所有六条染色体, // ,对于X坐标为0,对于Y坐标为1 Random rnd = new Random(); // 创建随机数(0或1) calculateCellsCenters( 200 , 325 ); // 计算分区区域(网格)的所有单元格的所有中心点(Xc,Yc) auvChromosomes = new int [ 5 , 5 ]; for ( int i = 0 ; i < population.Length; i ++) { population [i] = new int [ 5 , 5 ]; } prevRandomIndex = 0 ; randomIndex = 0 ; 使用(StreamWriter sw = new StreamWriter(fs)) { for ( int i = 0 ; i < population.Length; i ++) { for ( int j = 0 ; j < 5 ; j ++) { for ( int k = 0 ; k < 5 ; k ++) { auvChromosomes [j ,k] = rnd.Next( 0 , 2 ); sw.Write( | + auvChromosomes [j,k] + |); } // end-inner-for sw.WriteLine(); } // end-outer-inner-for while (prevRandomIndex == randomIndex) { randomIndex = rnd.Next( 0 , 5 ); // 从cellsCenters数组中选择随机X或Y坐标,用于每个染色体中的AUV随机情况 } auvChromosomeLocations [i, 0 ] = cellsCenters [ 0 ,randomIndex] ; auvChromosomeLocations [i, 1 ] = cellsCenters [ 1 ,randomIndex]; prevRandomIndex = randomIndex; sw.WriteLine( #------------ # + 染色体的AUV位置#: + i + = + ( + auvChromosomeLocations [i,0 + , + auvChromosomeLocations [i, 1 ] + ) + #----------- - #); } // end-outer-for } // end-using // Genetic_Algorithm(人口); } public void calculateCellsCenters( int intialPoint, int lastPoint) { double diagonalLength; FileStream fs = new FileStream( C:/Users/Welcome/Desktop/testPositions.txt,FileMode.Append,FileAccess.Write); int d = lastPoint - intialPoint; int cellSide = d / 5; // 获取矩阵5 * 5,因为我们的区域是125 * 125 int intialCell = intialPoint - cellSide; // 获取我们的两个数组(列和行)中第一个元素的初始值小于200 case int centerPoint; columnsXs = new int [ 6 ]; cellsCenters = new int [ 2 , 5 ]; // 0表示x轴上的点,1表示y轴上的点,所以这就是第一个元素的原因大小为2 rowsYs = new int [ 6 ]; diagonalLength = cellSide * Math.Sqrt( 2 ); centerPoint =( int )diagonalLength / 2 ; columnsXs [ 0 ] = 200 ; // 在我们的情况下它等于175,让第一列的第一个点等于200以后 / * *计算列数 * * * / for ( int k = 1 ; k < 6 ; k ++) { columnsXs [k] = columnsXs [k-1] + cellSide; } rowsYs [ 0 ] = 200 ; // 在我们的情况下它等于175,让第一列的第一个品脱等于200以后 / * *计算行数 * * * / for ( int s = 1 ; s < 6 ; s ++) { rowsYs [s] = rowsYs [s-1] + cellSide; } /// ///////////////////////计算,中心////////////////////// ///// 使用(StreamWriter sw = new StreamWriter(fs)) { for ( int i = 0 ; i < 5 ; i ++) { for ( int j = 0 ; j < 5 ; j ++) { cellsCenters [ 0 ,j] = columnsXs [j] + centerPoint; cellsCenters [ 1 ,j] = rowsYs [i] + centerPoint; sw.Write( + ( + cellsCenters [ 0 ,j] + , + cellsCenters [ 1 , j] + ) + ); } sw.WriteLine(); } } } 我在txt文件上打印了以下结果: | 0 || 1 || 0 || 0 || 0 | | 0 || 0 || 1 || 1 || 0 | | 1 || 1 || 1 || 1 || 1 | | 0 || 1 || 1 || 1 || 0 | | 0 || 1 || 0 || 0 || 0 | #------------#AUV染色体位置#:0 =(267,317)#------------# | 0 | | 0 || 0 || 1 || 0 | | 0 || 1 || 1 || 0 || 0 | | 0 || 1 || 1 || 0 || 0 | | 0 || 0 || 0 || 1 || 0 | | 1 || 0 || 0 || 1 || 0 | #------------#AUV染色体位置#:1 =(292,317)#------------# | 1 | | 0 || 0 || 0 || 1 | | 1 || 0 || 1 || 1 || 1 | | 1 || 0 || 0 || 1 || 0 | | 1 || 0 || 0 || 0 || 0 | | 1 || 1 || 1 || 1 || 1 | #------------#AUV染色体位置#:2 =(317,317)#------------# | 0 | | 1 || 1 || 1 || 0 | | 1 || 1 || 0 || 0 || 0 | | 1 || 0 || 1 || 0 || 1 | | 0 || 0 || 0 || 1 || 0 | | 0 || 0 || 0 || 0 || 0 | #------------#AUV染色体位置#:3 =(292,317)#------------# | 0 | | 0 || 0 || 0 || 0 | | 0 || 1 || 1 || 1 || 0 | | 0 || 0 || 1 || 1 || 1 | | 0 || 0 || 1 || 1 || 0 | | 1 || 0 || 0 || 1 || 1 | #------------#AUV染色体位置#:4 =(242,317)#------------# | 0 | | 1 || 1 || 1 || 1 | | 0 || 0 || 1 || 1 || 0 | | 0 || 0 || 0 || 1 || 0 | | 0 || 1 || 0 || 1 || 1 | | 1 || 0 || 1 || 1 || 0 | #------------#AUV染色体位置#:5 =(292,317)#------------# 我有以下位置(来自calculateCellsCenters方法)我需要从中选择: (217,217)(242,217)(267,217)(292,217)(317,217)(217,242)(242,242)(267,242)(292,242)(317,242)(217,267) )(242,267)(267,267)(292,267)(317,267)(217,292)(242,292)(267,292)(292,292)(317,292)(217,317)(242,317)(267,317)(292,317)(317,317) ) 我尝试过: 我需要为每个染色体获得AUV的唯一位置。我需要每次从cellsCenters数组中随机选择位置。解决方案 这个逻辑很简单。每次获得一个随机数时,您需要根据以前选择的数字列表进行检查,看它是否是唯一的。因此,创建一个简单的数组,每次检查值是否在数组中。如果它已经存在,那么它不是唯一的,所以选择另一个并重复该过程。如果它在数组中不存在那么它是唯一的,所以在使用之前将它添加到数组中。 [edit] CodeProject似乎存在问题,所有评论都在消失,或者没有保存。 [/ edit] I wrote a code to generate a population for a genetic algorithm in which each chromosome is a matrix of size 5 * 5 where each cell is associated with the coordinates generated by calling the method "calculateCellsCenters(200, 325)". For each chromosome I need to generate a random location for an object called AUV where this random location should be selected from the locations generated by that method. I need the location of AUV to be unique for each chromosome, but unfortunately I got a duplication and I think also that the selected locations are selected from not the whole available options but only from a part. Any idea??using System;using System.IO;using System.Text;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data;using System.Threading;using AUV_Topology;using System.Collections.Generic;using System.Media;using System.Linq;static int[,] auvChromosomes;static int[][,] population;static int[,] auvChromosomeLocations;static int randomIndex; // to select random X or Y coordinates from the array of cellsCenters for the AUV random loaction in each chromosomestatic int prevRandomIndex; // to make sure that each time we will get different randomIndex;static int[,] cellsCenters;static int[] columnsXs;static int[] rowsYs; private void moveGenetic_Click(object sender, EventArgs e) { FileStream fs = new FileStream("C:/Users/Welcome/Desktop/intialPopulation.txt", FileMode.Append,FileAccess.Write); population = new int[6][,]; // jagged array auvChromosomeLocations = new int[6, 2]; // for all the six chromosomes that will be generated, 0 for X coordinate and 1 for Y coordinate Random rnd = new Random(); // to gereate random number (either 0 or 1) calculateCellsCenters(200, 325); // Compute all the center points (Xc,Yc) for all cells of the partitioned region (grid) auvChromosomes = new int[5, 5]; for (int i = 0; i < population.Length; i++) { population[i] = new int[5, 5]; } prevRandomIndex = 0; randomIndex = 0; using (StreamWriter sw = new StreamWriter(fs)) { for (int i = 0; i < population.Length; i++) { for (int j = 0; j < 5; j++) { for (int k = 0; k < 5; k++) { auvChromosomes[j, k] = rnd.Next(0, 2); sw.Write("|" + auvChromosomes[j, k] + "|"); } // end-inner-for sw.WriteLine(); } // end-outer-inner-for while (prevRandomIndex == randomIndex) { randomIndex = rnd.Next(0, 5); // to select random X or Y coordinates from the array of cellsCenters for the AUV random loaction in each chromosome } auvChromosomeLocations[i, 0] = cellsCenters[0, randomIndex]; auvChromosomeLocations[i, 1] = cellsCenters[1, randomIndex]; prevRandomIndex = randomIndex; sw.WriteLine("#------------#" + "AUV Location for chromosome # : " + i + " =" + " (" + auvChromosomeLocations [i, 0+ "," +auvChromosomeLocations [i, 1] + ")" + "#------------#"); } // end-outer-for } // end-using //Genetic_Algorithm(population); }public void calculateCellsCenters(int intialPoint,int lastPoint) { double diagonalLength; FileStream fs = new FileStream("C:/Users/Welcome/Desktop/testPositions.txt", FileMode.Append, FileAccess.Write); int d = lastPoint - intialPoint; int cellSide = d /5; // to get matrix 5* 5 as our region in our case is 125 * 125 int intialCell = intialPoint - cellSide; // to get the intial value of the first element in the two arrays (columns & rows) less than 200 in our case int centerPoint; columnsXs = new int[6]; cellsCenters = new int[2, 5]; // 0 for the point on the x axis and 1 for the point on the y axis so this is why the first element of size 2 rowsYs = new int[6]; diagonalLength = cellSide * Math.Sqrt(2); centerPoint = (int) diagonalLength / 2; columnsXs[0] = 200; // its equal to 175 in our case to let the first point for the first column equal to 200 later on /* * Calculate The Columns Points * * */ for (int k = 1; k < 6; k++) { columnsXs[k] = columnsXs[k-1] + cellSide; } rowsYs[0] = 200; // its equal to 175 in our case to let the first pint for the first column equal to 200 later on /* * Calculate The Rows Points * * */ for (int s = 1; s < 6; s++) { rowsYs[s] = rowsYs[s-1] + cellSide; } //////////////////////////calculate-centers/////////////////////////// using (StreamWriter sw = new StreamWriter(fs)) { for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { cellsCenters[0, j] = columnsXs[j] + centerPoint; cellsCenters[1, j] = rowsYs[i] + centerPoint; sw.Write(" " + "(" + cellsCenters[0, j] + "," + cellsCenters[1, j] + ")" + " " ); } sw.WriteLine(); } } }I got the following results printed on a txt file:|0||1||0||0||0||0||0||1||1||0||1||1||1||1||1||0||1||1||1||0||0||1||0||0||0|#------------#AUV Location for chromosome # : 0 = (267,317)#------------#|0||0||0||1||0||0||1||1||0||0||0||1||1||0||0||0||0||0||1||0||1||0||0||1||0|#------------#AUV Location for chromosome # : 1 = (292,317)#------------#|1||0||0||0||1||1||0||1||1||1||1||0||0||1||0||1||0||0||0||0||1||1||1||1||1|#------------#AUV Location for chromosome # : 2 = (317,317)#------------#|0||1||1||1||0||1||1||0||0||0||1||0||1||0||1||0||0||0||1||0||0||0||0||0||0|#------------#AUV Location for chromosome # : 3 = (292,317)#------------#|0||0||0||0||0||0||1||1||1||0||0||0||1||1||1||0||0||1||1||0||1||0||0||1||1|#------------#AUV Location for chromosome # : 4 = (242,317)#------------#|0||1||1||1||1||0||0||1||1||0||0||0||0||1||0||0||1||0||1||1||1||0||1||1||0|#------------#AUV Location for chromosome # : 5 = (292,317)#------------#I got the following locations (from "calculateCellsCenters" method) that I need to choose from them: (217,217) (242,217) (267,217) (292,217) (317,217)(217,242) (242,242) (267,242) (292,242) (317,242)(217,267) (242,267) (267,267) (292,267) (317,267)(217,292) (242,292) (267,292) (292,292) (317,292)(217,317) (242,317) (267,317) (292,317) (317,317)What I have tried:I need to get a unique location for the AUV for each chromosome. I need to select the location randomly each time from "cellsCenters" array. 解决方案 The logic for this is quite straightforward. Each time you get a random number you need to check it against a list of previously selected numbers to see if it is unique. So create a simple array and each time check if the value is in the array. If it is already there then it is not unique, so select another one and repeat the process. If it does not exist in the array then it is unique, so add it to the array before using it.[edit]There appears to be a problem at CodeProject and all comments are disappearing, or not being saved.[/edit] 这篇关于如何从整数数组中选择一个随机数,并使其每次都是唯一的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-13 16:14