本文整理汇总了C#中System.Random.NextBoolean方法的典型用法代码示例。如果您正苦于以下问题:C# Random.NextBoolean方法的具体用法?C# Random.NextBoolean怎么用?C# Random.NextBoolean使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Random
的用法示例。
在下文中一共展示了Random.NextBoolean方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GenerateRandomAccountCollection
private EntityCollection GenerateRandomAccountCollection()
{
var collection = new List<Entity>();
for (var i = 0; i < 10; i++)
{
var rgn = new Random((int)DateTime.Now.Ticks);
var entity = new Entity("account");
entity["accountid"] = entity.Id = Guid.NewGuid();
entity["address1_addressid"] = Guid.NewGuid();
entity["modifiedon"] = DateTime.Now;
entity["lastusedincampaign"] = DateTime.Now;
entity["donotfax"] = rgn.NextBoolean();
entity["new_verybignumber"] = rgn.NextInt64();
entity["exchangerate"] = rgn.NextDecimal();
entity["address1_latitude"] = rgn.NextDouble();
entity["numberofemployees"] = rgn.NextInt32();
entity["primarycontactid"] = new EntityReference("contact", Guid.NewGuid());
entity["revenue"] = new Money(rgn.NextDecimal());
entity["ownerid"] = new EntityReference("systemuser", Guid.NewGuid());
entity["industrycode"] = new OptionSetValue(rgn.NextInt32());
entity["name"] = rgn.NextString(15);
entity["description"] = rgn.NextString(300);
entity["statecode"] = new OptionSetValue(rgn.NextInt32());
entity["statuscode"] = new OptionSetValue(rgn.NextInt32());
collection.Add(entity);
}
return new EntityCollection(collection);
}
示例2: GenerateStream
private IObservable<MarketData> GenerateStream(CurrencyPair currencyPair)
{
return Observable.Create<MarketData>(observer =>
{
var spread = currencyPair.DefaultSpread;
var midRate = currencyPair.InitialPrice;
var bid = midRate - (spread * currencyPair.PipSize);
var offer = midRate + (spread * currencyPair.PipSize);
var initial = new MarketData(currencyPair.Code, bid, offer);
var currentPrice = initial;
observer.OnNext(initial);
var random = new Random();
//for a given period, move prices by up to 5 pips
return Observable.Interval(TimeSpan.FromSeconds(1 / (double)currencyPair.TickFrequency))
.Select(_ => random.Next(1, 5))
.Subscribe(pips =>
{
//move up or down between 1 and 5 pips
var adjustment = Math.Round(pips * currencyPair.PipSize,currencyPair.DecimalPlaces);
currentPrice = random.NextBoolean()
? currentPrice + adjustment
: currentPrice - adjustment;
observer.OnNext(currentPrice);
});
});
}
示例3: MultipleCreateAndDeleteTest
public void MultipleCreateAndDeleteTest()
{
//const int N = 21;
using (var scope = new TempLinksTestScope())
{
var links = scope.Links;
for (var N = 0; N < 100; N++)
{
var random = new Random(N);
var created = 0;
var deleted = 0;
for (var i = 0; i < N; i++)
{
var linksCount = links.Count();
var createPoint = random.NextBoolean();
if (linksCount > 2 && createPoint)
{
var source = random.NextUInt64(1, linksCount);
var target = random.NextUInt64(1, linksCount);
var resultLink = links.CreateAndUpdate(source, target);
if (resultLink > linksCount)
created++;
}
else
{
links.Create();
created++;
}
}
Assert.True(created == (int)links.Count());
for (var i = 0; i < N; i++)
{
var link = (ulong)i + 1;
if (links.Exists(link))
{
links.Delete(link);
deleted++;
}
}
Assert.True(links.Count() == 0);
}
}
}
示例4: UnitTestBase
protected UnitTestBase()
{
List<TestEntity> entities = new List<TestEntity>();
DateTime dt = DateTime.Now;
Random rnd = new Random();
for (int i = 0; i < 1000; i++)
{
entities.Add(new TestEntity()
{
Id = i + 1,
Name = "Name" + (i + 1),
AddDate = rnd.NextDateTime(dt.AddDays(-7), dt.AddDays(7)),
IsDeleted = rnd.NextBoolean()
});
}
Entities = entities;
}
示例5: NewLogMergePolicy
public static LogMergePolicy NewLogMergePolicy(Random r)
{
LogMergePolicy logmp = r.NextBoolean() ? (LogMergePolicy)new LogDocMergePolicy() : new LogByteSizeMergePolicy();
logmp.CalibrateSizeByDeletes = r.NextBoolean();
if (Rarely(r))
{
logmp.MergeFactor = TestUtil.NextInt(r, 2, 9);
}
else
{
logmp.MergeFactor = TestUtil.NextInt(r, 10, 50);
}
ConfigureRandom(r, logmp);
return logmp;
}
示例6: NewMergePolicy
public static MergePolicy NewMergePolicy(Random r)
{
if (Rarely(r))
{
return new MockRandomMergePolicy(r);
}
else if (r.NextBoolean())
{
return NewTieredMergePolicy(r);
}
else if (r.Next(5) == 0)
{
return NewAlcoholicMergePolicy(r, ClassEnvRule.TimeZone);
}
return NewLogMergePolicy(r);
}
示例7: NewIndexWriterConfig
/// <summary>
/// create a new index writer config with random defaults using the specified random </summary>
public static IndexWriterConfig NewIndexWriterConfig(Random r, LuceneVersion v, Analyzer a)
{
IndexWriterConfig c = new IndexWriterConfig(v, a);
c.SetSimilarity(ClassEnvRule.Similarity);
if (VERBOSE)
{
// Even though TestRuleSetupAndRestoreClassEnv calls
// InfoStream.setDefault, we do it again here so that
// the PrintStreamInfoStream.messageID increments so
// that when there are separate instances of
// IndexWriter created we see "IW 0", "IW 1", "IW 2",
// ... instead of just always "IW 0":
c.InfoStream = new TestRuleSetupAndRestoreClassEnv.ThreadNameFixingPrintStreamInfoStream(Console.Out);
}
if (r.NextBoolean())
{
c.SetMergeScheduler(new SerialMergeScheduler());
}
else if (Rarely(r))
{
int maxThreadCount = TestUtil.NextInt(Random(), 1, 4);
int maxMergeCount = TestUtil.NextInt(Random(), maxThreadCount, maxThreadCount + 4);
ConcurrentMergeScheduler cms = new ConcurrentMergeScheduler();
cms.SetMaxMergesAndThreads(maxMergeCount, maxThreadCount);
c.SetMergeScheduler(cms);
}
if (r.NextBoolean())
{
if (Rarely(r))
{
// crazy value
c.SetMaxBufferedDocs(TestUtil.NextInt(r, 2, 15));
}
else
{
// reasonable value
c.SetMaxBufferedDocs(TestUtil.NextInt(r, 16, 1000));
}
}
if (r.NextBoolean())
{
if (Rarely(r))
{
// crazy value
c.SetTermIndexInterval(r.NextBoolean() ? TestUtil.NextInt(r, 1, 31) : TestUtil.NextInt(r, 129, 1000));
}
else
{
// reasonable value
c.SetTermIndexInterval(TestUtil.NextInt(r, 32, 128));
}
}
if (r.NextBoolean())
{
int maxNumThreadStates = Rarely(r) ? TestUtil.NextInt(r, 5, 20) : TestUtil.NextInt(r, 1, 4); // reasonable value - crazy value
if (Rarely(r))
{
// Retrieve the package-private setIndexerThreadPool
// method:
MethodInfo setIndexerThreadPoolMethod = typeof(IndexWriterConfig).GetMethod("SetIndexerThreadPool", new Type[] { typeof(DocumentsWriterPerThreadPool) });
//setIndexerThreadPoolMethod.setAccessible(true);
Type clazz = typeof(RandomDocumentsWriterPerThreadPool);
ConstructorInfo ctor = clazz.GetConstructor(new[] { typeof(int), typeof(Random) });
//ctor.Accessible = true;
// random thread pool
setIndexerThreadPoolMethod.Invoke(c, new[] { ctor.Invoke(new object[] { maxNumThreadStates, r }) });
}
else
{
// random thread pool
c.SetMaxThreadStates(maxNumThreadStates);
}
}
c.SetMergePolicy(NewMergePolicy(r));
if (Rarely(r))
{
c.SetMergedSegmentWarmer(new SimpleMergedSegmentWarmer(c.InfoStream));
}
c.SetUseCompoundFile(r.NextBoolean());
c.SetReaderPooling(r.NextBoolean());
c.SetReaderTermsIndexDivisor(TestUtil.NextInt(r, 1, 4));
c.SetCheckIntegrityAtMerge(r.NextBoolean());
return c;
}
示例8: NewIOContext
/// <summary>
/// TODO: javadoc </summary>
public static IOContext NewIOContext(Random random, IOContext oldContext)
{
int randomNumDocs = random.Next(4192);
int size = random.Next(512) * randomNumDocs;
if (oldContext.FlushInfo != null)
{
// Always return at least the estimatedSegmentSize of
// the incoming IOContext:
return new IOContext(new FlushInfo(randomNumDocs, (long)Math.Max(oldContext.FlushInfo.EstimatedSegmentSize, size)));
}
else if (oldContext.MergeInfo != null)
{
// Always return at least the estimatedMergeBytes of
// the incoming IOContext:
return new IOContext(new MergeInfo(randomNumDocs, Math.Max(oldContext.MergeInfo.EstimatedMergeBytes, size), random.NextBoolean(), TestUtil.NextInt(random, 1, 100)));
}
else
{
// Make a totally random IOContext:
IOContext context;
switch (random.Next(5))
{
case 0:
context = IOContext.DEFAULT;
break;
case 1:
context = IOContext.READ;
break;
case 2:
context = IOContext.READONCE;
break;
case 3:
context = new IOContext(new MergeInfo(randomNumDocs, size, true, -1));
break;
case 4:
context = new IOContext(new FlushInfo(randomNumDocs, size));
break;
default:
context = IOContext.DEFAULT;
break;
}
return context;
}
}
示例9: TestRuleSetupAndRestoreClassEnv
public TestRuleSetupAndRestoreClassEnv()
{
/*// if verbose: print some debugging stuff about which codecs are loaded.
if (LuceneTestCase.VERBOSE)
{
ISet<string> codecs = Codec.AvailableCodecs();
foreach (string codec in codecs)
{
Console.WriteLine("Loaded codec: '" + codec + "': " + Codec.ForName(codec).GetType().Name);
}
ISet<string> postingsFormats = PostingsFormat.AvailablePostingsFormats();
foreach (string postingsFormat in postingsFormats)
{
Console.WriteLine("Loaded postingsFormat: '" + postingsFormat + "': " + PostingsFormat.ForName(postingsFormat).GetType().Name);
}
}
SavedInfoStream = InfoStream.Default;
Random random = RandomizedContext.Current.Random;
bool v = random.NextBoolean();
if (LuceneTestCase.INFOSTREAM)
{
InfoStream.Default = new ThreadNameFixingPrintStreamInfoStream(Console.Out);
}
else if (v)
{
InfoStream.Default = new NullInfoStream();
}
Type targetClass = RandomizedContext.Current.GetTargetType;
AvoidCodecs = new HashSet<string>();
// set back to default
LuceneTestCase.OLD_FORMAT_IMPERSONATION_IS_ACTIVE = false;
SavedCodec = Codec.Default;
int randomVal = random.Next(10);
if ("Lucene3x".Equals(LuceneTestCase.TEST_CODEC) || ("random".Equals(LuceneTestCase.TEST_CODEC) && "random".Equals(LuceneTestCase.TEST_POSTINGSFORMAT) && "random".Equals(LuceneTestCase.TEST_DOCVALUESFORMAT) && randomVal == 3 && !ShouldAvoidCodec("Lucene3x"))) // preflex-only setup
{
Codec = Codec.ForName("Lucene3x");
Debug.Assert((Codec is PreFlexRWCodec), "fix your classpath to have tests-framework.jar before lucene-core.jar");
LuceneTestCase.OLD_FORMAT_IMPERSONATION_IS_ACTIVE = true;
}
else if ("Lucene40".Equals(LuceneTestCase.TEST_CODEC) || ("random".Equals(LuceneTestCase.TEST_CODEC) && "random".Equals(LuceneTestCase.TEST_POSTINGSFORMAT) && randomVal == 0 && !ShouldAvoidCodec("Lucene40"))) // 4.0 setup
{
Codec = Codec.ForName("Lucene40");
LuceneTestCase.OLD_FORMAT_IMPERSONATION_IS_ACTIVE = true;
Debug.Assert(Codec is Lucene40RWCodec, "fix your classpath to have tests-framework.jar before lucene-core.jar");
Debug.Assert((PostingsFormat.ForName("Lucene40") is Lucene40RWPostingsFormat), "fix your classpath to have tests-framework.jar before lucene-core.jar");
}
else if ("Lucene41".Equals(LuceneTestCase.TEST_CODEC) || ("random".Equals(LuceneTestCase.TEST_CODEC) && "random".Equals(LuceneTestCase.TEST_POSTINGSFORMAT) && "random".Equals(LuceneTestCase.TEST_DOCVALUESFORMAT) && randomVal == 1 && !ShouldAvoidCodec("Lucene41")))
{
Codec = Codec.ForName("Lucene41");
LuceneTestCase.OLD_FORMAT_IMPERSONATION_IS_ACTIVE = true;
Debug.Assert(Codec is Lucene41RWCodec, "fix your classpath to have tests-framework.jar before lucene-core.jar");
}
else if ("Lucene42".Equals(LuceneTestCase.TEST_CODEC) || ("random".Equals(LuceneTestCase.TEST_CODEC) && "random".Equals(LuceneTestCase.TEST_POSTINGSFORMAT) && "random".Equals(LuceneTestCase.TEST_DOCVALUESFORMAT) && randomVal == 2 && !ShouldAvoidCodec("Lucene42")))
{
Codec = Codec.ForName("Lucene42");
LuceneTestCase.OLD_FORMAT_IMPERSONATION_IS_ACTIVE = true;
Debug.Assert(Codec is Lucene42RWCodec, "fix your classpath to have tests-framework.jar before lucene-core.jar");
}
else if ("Lucene45".Equals(LuceneTestCase.TEST_CODEC) || ("random".Equals(LuceneTestCase.TEST_CODEC) && "random".Equals(LuceneTestCase.TEST_POSTINGSFORMAT) && "random".Equals(LuceneTestCase.TEST_DOCVALUESFORMAT) && randomVal == 5 && !ShouldAvoidCodec("Lucene45")))
{
Codec = Codec.ForName("Lucene45");
LuceneTestCase.OLD_FORMAT_IMPERSONATION_IS_ACTIVE = true;
Debug.Assert(Codec is Lucene45RWCodec, "fix your classpath to have tests-framework.jar before lucene-core.jar");
}
else if (("random".Equals(LuceneTestCase.TEST_POSTINGSFORMAT) == false) || ("random".Equals(LuceneTestCase.TEST_DOCVALUESFORMAT) == false))
{
// the user wired postings or DV: this is messy
// refactor into RandomCodec....
PostingsFormat format;
if ("random".Equals(LuceneTestCase.TEST_POSTINGSFORMAT))
{
format = PostingsFormat.ForName("Lucene41");
}
else
{
format = PostingsFormat.ForName(LuceneTestCase.TEST_POSTINGSFORMAT);
}
DocValuesFormat dvFormat;
if ("random".Equals(LuceneTestCase.TEST_DOCVALUESFORMAT))
{
dvFormat = DocValuesFormat.ForName("Lucene45");
}
else
{
dvFormat = DocValuesFormat.ForName(LuceneTestCase.TEST_DOCVALUESFORMAT);
}
Codec = new Lucene46CodecAnonymousInnerClassHelper(this, format, dvFormat);
}
else if ("Asserting".Equals(LuceneTestCase.TEST_CODEC) || ("random".Equals(LuceneTestCase.TEST_CODEC) && randomVal == 6 && !ShouldAvoidCodec("Asserting")))
{
Codec = new AssertingCodec();
}
//.........这里部分代码省略.........
示例10: NewField
public static Field NewField(Random random, string name, string value, FieldType type)
{
name = new string(name.ToCharArray());
if (Usually(random) || !type.Indexed)
{
// most of the time, don't modify the params
return new Field(name, value, type);
}
// TODO: once all core & test codecs can index
// offsets, sometimes randomly turn on offsets if we are
// already indexing positions...
FieldType newType = new FieldType(type);
if (!newType.Stored && random.NextBoolean())
{
newType.Stored = true; // randomly store it
}
if (!newType.StoreTermVectors && random.NextBoolean())
{
newType.StoreTermVectors = true;
if (!newType.StoreTermVectorOffsets)
{
newType.StoreTermVectorOffsets = random.NextBoolean();
}
if (!newType.StoreTermVectorPositions)
{
newType.StoreTermVectorPositions = random.NextBoolean();
if (newType.StoreTermVectorPositions && !newType.StoreTermVectorPayloads && !OLD_FORMAT_IMPERSONATION_IS_ACTIVE)
{
newType.StoreTermVectorPayloads = random.NextBoolean();
}
}
}
// TODO: we need to do this, but smarter, ie, most of
// the time we set the same value for a given field but
// sometimes (rarely) we change it up:
/*
if (newType.OmitsNorms()) {
newType.setOmitNorms(random.NextBoolean());
}
*/
return new Field(name, value, newType);
}
示例11: GetRandomAcceptedString
public int[] GetRandomAcceptedString(Random r)
{
IList<int?> soFar = new List<int?>();
if (a.IsSingleton)
{
// accepts only one
var s = a.Singleton;
int charUpto = 0;
while (charUpto < s.Length)
{
int cp = Character.CodePointAt(s, charUpto);
charUpto += Character.CharCount(cp);
soFar.Add(cp);
}
}
else
{
var s = a.InitialState;
while (true)
{
if (s.Accept)
{
if (s.numTransitions == 0)
{
// stop now
break;
}
else
{
if (r.NextBoolean())
{
break;
}
}
}
if (s.numTransitions == 0)
{
throw new Exception("this automaton has dead states");
}
bool cheat = r.NextBoolean();
Transition t;
if (cheat)
{
// pick a transition that we know is the fastest
// path to an accept state
IList<Transition> toAccept = new List<Transition>();
for (int i = 0; i < s.numTransitions; i++)
{
Transition t0 = s.TransitionsArray[i];
if (LeadsToAccept.ContainsKey(t0))
{
toAccept.Add(t0);
}
}
if (toAccept.Count == 0)
{
// this is OK -- it means we jumped into a cycle
t = s.TransitionsArray[r.Next(s.numTransitions)];
}
else
{
t = toAccept[r.Next(toAccept.Count)];
}
}
else
{
t = s.TransitionsArray[r.Next(s.numTransitions)];
}
soFar.Add(GetRandomCodePoint(r, t));
s = t.Dest;
}
}
return ArrayUtil.ToIntArray(soFar);
}
示例12: RandomLong
private long RandomLong(Random random)
{
long val;
switch (random.Next(4))
{
case 0:
val = 1L << (random.Next(63)); // patterns like 0x000000100000 (-1 yields patterns like 0x0000fff)
break;
case 1:
val = -1L << (random.Next(63)); // patterns like 0xfffff00000
break;
default:
val = random.NextLong();
break;
}
val += random.Next(5) - 2;
if (random.NextBoolean())
{
if (random.NextBoolean())
{
val += random.Next(100) - 50;
}
if (random.NextBoolean())
{
val = ~val;
}
if (random.NextBoolean())
{
val = val << 1;
}
if (random.NextBoolean())
{
val = (long)((ulong)val >> 1);
}
}
return val;
}
示例13: ConfigureRandom
private static void ConfigureRandom(Random r, MergePolicy mergePolicy)
{
if (r.NextBoolean())
{
mergePolicy.NoCFSRatio = 0.1 + r.NextDouble() * 0.8;
}
else
{
mergePolicy.NoCFSRatio = r.NextBoolean() ? 1.0 : 0.0;
}
if (Rarely())
{
mergePolicy.MaxCFSSegmentSizeMB = 0.2 + r.NextDouble() * 2.0;
}
else
{
mergePolicy.MaxCFSSegmentSizeMB = double.PositiveInfinity;
}
}
示例14: AssertingWeight
internal AssertingWeight(Random random, Weight @in)
{
this.Random = random;
[email protected] = @in;
ScoresDocsOutOfOrder_Renamed = @in.ScoresDocsOutOfOrder() || random.NextBoolean();
}
示例15: SeedPostings
public SeedPostings(long seed, int minDocFreq, int maxDocFreq, Bits liveDocs, FieldInfo.IndexOptions options)
{
Random = new Random((int)seed);
DocRandom = new Random(Random.Next());
DocFreq = TestUtil.NextInt(Random, minDocFreq, maxDocFreq);
this.LiveDocs = liveDocs;
// TODO: more realistic to inversely tie this to numDocs:
MaxDocSpacing = TestUtil.NextInt(Random, 1, 100);
if (Random.Next(10) == 7)
{
// 10% of the time create big payloads:
PayloadSize = 1 + Random.Next(3);
}
else
{
PayloadSize = 1 + Random.Next(1);
}
FixedPayloads = Random.NextBoolean();
var payloadBytes = new byte[PayloadSize];
Payload_Renamed = new BytesRef(payloadBytes);
this.Options = options;
DoPositions = FieldInfo.IndexOptions.DOCS_AND_FREQS_AND_POSITIONS.CompareTo(options) <= 0;
}