I2PIdentHash GetRandomRouter(RouletteSelection <I2PRouterInfo, I2PIdentHash> r, bool exploratory) { I2PIdentHash result; var me = RouterContext.Inst.MyRouterIdentity.IdentHash; if (exploratory) { lock ( RouterInfos ) { do { lock (r.Wheel) { result = Roulette.Wheel.Random().Id; } } while (result == me); return(result); } } do { var one = r.GetWeightedRandom(); var retries = 0; while (!Contains(one) && ++retries < 20) { one = r.GetWeightedRandom(); } result = one; } while (result == me); return(result); }
private I2PIdentHash GetRandomRouter( RouletteSelection <I2PRouterInfo, I2PIdentHash> r, ConcurrentBag <I2PIdentHash> exclude, bool exploratory) { I2PIdentHash result; var me = RouterContext.Inst.MyRouterIdentity.IdentHash; if (exploratory) { var subset = RouterInfos .Where(k => !exclude.Contains(k.Key)); do { result = subset .Random() .Key; } while (result == me); return(result); } var retries = 0; bool tryagain; do { result = r.GetWeightedRandom(r.Wheel.Count() < 300 ? null : exclude); tryagain = result == me; } while (tryagain && ++retries < 20); //Logging.LogInformation( $"GetRandomRouter selected {result}: {exclude.Any( k2 => k2 == result )}" ); return(result); }
private I2PIdentHash GetRandomRouter( RouletteSelection <I2PRouterInfo, I2PIdentHash> r, IEnumerable <I2PIdentHash> exclude, bool exploratory) { I2PIdentHash result; var me = RouterContext.Inst.MyRouterIdentity.IdentHash; var excludeset = new HashSet <I2PIdentHash>(exclude); if (exploratory) { lock ( RouterInfos ) { var subset = RouterInfos .Where(k => !excludeset.Contains(k.Key)); do { result = subset .Random() .Key; } while (result == me); return(result); } } var retries = 0; bool tryagain; do { result = r.GetWeightedRandom(excludeset); tryagain = result == me; } while (tryagain && ++retries < 20); //Logging.LogInformation( $"GetRandomRouter selected {result}: {exclude.Any( k2 => k2 == result )}" ); return(result); }