/// <summary> /// AlmostOrdered will return an array of integers that are almost-ordered /// </summary> /// <param name="n">The length of the array of integers to be returned.</param> /// <param name="p">The percentage of numbers in the array that are out of place.</param> /// <returns>An array of n numbers, 1 to n (no duplicates). (100-p)% of the numbers are in their correct order. /// p% of the numbers are out of place.</returns> /// <example>If n = 200, and p = 3, then AlmostOrdered produces an array of size 100. /// These numbers are sorted, but 3 (or 3%) numbers from the sequence are moved to a different place.</example> /// /// Adapted from https://stackoverflow.com/questions/37419736/almost-ordered-not-sorting-the-exact-amount-of-values-i-give-it int[] AlmostOrdered(int n, double p) { IRandomProvider random = new RandomAdapter(); if (p > 100) { throw new InvalidOperationException("Cannot shuffle more than 100% of the numbers"); } int shuffled = 0; // Create and Populate an array int[] array = new int[n]; for (int i = 1; i <= n; i++) { array[i - 1] = i; } // Calculate numbers to shuffle int numsOutOfPlace = (int)Math.Round(n * (p / 100)); long firstRandomIndex = 0; long secondRandomIndex = 0; do { firstRandomIndex = random.Next(n - 1); // to make sure that the two numbers are not the same do { secondRandomIndex = random.Next(n - 1); } while (firstRandomIndex == secondRandomIndex); int temp = array[firstRandomIndex]; array[firstRandomIndex] = array[secondRandomIndex]; array[secondRandomIndex] = temp; shuffled++; } // Shuffles and adds numbers to the array while the length of the array is less than the numbers out of place calculated. while (shuffled < numsOutOfPlace); return(array); }
public InvokeResult <string> SendWXMessageSchedule() { InvokeResult <string> result = new InvokeResult <string>(); try { IList <WXSend> messagesToSend = BuilderFactory.DefaultBulder(GlobalManager.getConnectString()).ListByPage <WXSend>(new WXSend { Status = 0 }.ToStringObjectDictionary(false), new ListPager { PageNo = 1, PageSize = 10, OrderByClause = "ScheduleTime" }); if (messagesToSend.Count == 0) { //无消息需要发送 result.ret = "无消息需要发送"; } else { string batchNum = RandomAdapter.GetRandomString(8); List <IBatisNetBatchStatement> statements = new List <IBatisNetBatchStatement>(); List <string> errMsgs = new List <string>(); foreach (WXSend messageToSend in messagesToSend) { string payload; if (messageToSend.SendCatalog.ToLower() == WXRequestMessageType.text.ToString()) { payload = new WXRequestTextMessage { touser = messageToSend.toUserName, msgtype = WXRequestMessageType.text.ToString(), text = new WXTextMessageWrapper { content = messageToSend.SendContent.ReplaceEmoji() } }.ToJson(); } else if (messageToSend.SendCatalog.ToLower() == WXRequestMessageType.news.ToString()) { WXRequestNewsItem newsItem = JsonConvert.DeserializeObject <WXRequestNewsItem>(messageToSend.SendContent.ReplaceEmoji()); payload = new WXRequestNewsMessage { touser = messageToSend.toUserName, msgtype = WXRequestMessageType.news.ToString(), news = new WXNewsMessageWrapper { articles = new List <WXRequestNewsItem> { newsItem } } }.ToJson(); } else { payload = ""; } _SendWXMessage(payload, () => { statements.Add(new IBatisNetBatchStatement { StatementName = messageToSend.GetUpdateMethodName(), ParameterObject = new { Id = messageToSend.Id, BatchNum = batchNum, SendTime = DateTime.Now, Status = 1, SendResult = 0 }.ToStringObjectDictionary(), Type = SqlExecuteType.UPDATE }); }, (errCode, errMsg) => { errMsgs.Add(errMsg); statements.Add(new IBatisNetBatchStatement { StatementName = messageToSend.GetUpdateMethodName(), ParameterObject = new { Id = messageToSend.Id, BatchNum = batchNum, SendTime = DateTime.Now, Status = 2, SendResult = errCode }.ToStringObjectDictionary(), Type = SqlExecuteType.UPDATE }); }); } if (statements.Count > 0) { BuilderFactory.DefaultBulder(GlobalManager.getConnectString()).ExecuteNativeSqlNoneQuery(statements); } result.ret = "发送成功->发送批号:" + batchNum; if (errMsgs.Count > 0) { result.ret = "发送失败->发送批号:" + batchNum + " 错误:" + string.Join(";", errMsgs.Distinct().ToArray()); } } } catch (Exception ex) { result.ErrorCode = 59997; result.ErrorMessage = ex.Message; } return(result); }