string str = "select tpcID from tb_Topic where sbjID="+id;
DataTable dt = db.GetDataSet(str);
int num = dt.Rows.Count;
Random rd = new Random();
int i = rd.Next(0,num-1);
return dt.Rows[i][0].ToString();
}
------解决方案-------------------- 存储到集合中,判断集合中是否存在,存在就继续随机
------解决方案-------------------- 可以先生成一个随机数组再查询。 public int[] GetRandomUnrepeatArray(int minValue, int maxValue, int count) {
Random rnd = new Random(); // rnd.Next(minValue,maxValue) int length = maxValue - minValue + 1; byte[] keys = new byte[length]; rnd.NextBytes(keys); int[] items = new int[length]; for (int i = 0; i < length; i++) { items[i] = i + minValue; } Array.Sort(keys, items); int[] result = new int[count]; Array.Copy(items, result, count); return result; }
------解决方案-------------------- System.Threading.Thread.Sleep(1); 让它休息会 嘿嘿
------解决方案-------------------- 存到List中,每次取出来数值后跟List遍历对比,重复就break,重新随机...
------解决方案-------------------- int randomsheet = uncheck((int)DateTime.Now.Ticks);
Random rd = new Random(randonsheet);
------解决方案-------------------- 楼上很详细了
------解决方案-------------------- 随机代码有问题,调试看看
------解决方案-------------------- List<int> iList = new List<int>(); Random r = new Random(); int i = rd.Next(0,num-1); if(!iList.Contains(i)) { list.Add(i); return dt.Rows[i][0].ToString(); }