本文整理汇总了C#中System.Random.NextBool方法的典型用法代码示例。如果您正苦于以下问题:C# Random.NextBool方法的具体用法?C# Random.NextBool怎么用?C# Random.NextBool使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Random
的用法示例。
在下文中一共展示了Random.NextBool方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CheckIntersectionCore
private void CheckIntersectionCore(int capacity, Random r)
{
BitVector b1 = BitVector.Empty, b2 = BitVector.Empty;
b1.EnsureCapacity(capacity);
b2.EnsureCapacity(capacity);
bool[] a1 = new bool[capacity], a2 = new bool[capacity];
for (int i = 0; i < capacity; i++)
{
b1[i] = a1[i] = r.NextBool();
b2[i] = a2[i] = r.NextBool();
}
bool changed = b1.IntersectWith(b2);
bool changed2 = false;
for (int i = 0; i < capacity; i++)
{
bool a = a1[i];
a1[i] &= a2[i];
changed2 |= (a != a1[i]);
}
for (int i = 0; i < capacity; i++)
{
Assert.Equal(a1[i], b1[i]);
}
Assert.Equal(changed, changed2);
}
示例2: TestRandom
private static void TestRandom()
{
long time = DateTime.Now.Ticks;
Random rand = new Random();
int t = 0, f = 0;
for (int i = 0; i < 10000; i++)
{
bool b = rand.NextBool();
if (b)
{
t++;
}
else
{
f++;
}
}
VersatileIO.WriteComplex("&aTRUE: {0}\n&cFALSE: {1}".Fmt(t, f), '&');
long elapsedTicks = DateTime.Now.Ticks - time;
TimeSpan dtime = TimeSpan.FromTicks(elapsedTicks);
VersatileIO.WriteLine("Elapsed time: {0} ms".Fmt(dtime.TotalMilliseconds));
}
示例3: RandomUdpTest
public void RandomUdpTest()
{
EthernetLayer ethernetLayer = new EthernetLayer
{
Source = new MacAddress("00:01:02:03:04:05"),
Destination = new MacAddress("A0:A1:A2:A3:A4:A5")
};
int seed = new Random().Next();
Console.WriteLine("Seed: " + seed);
Random random = new Random(seed);
for (int i = 0; i != 1000; ++i)
{
IpV4Layer ipV4Layer = random.NextIpV4Layer(null);
ipV4Layer.HeaderChecksum = null;
IpV6Layer ipV6Layer = random.NextIpV6Layer(IpV4Protocol.Udp, false);
EthernetType ethernetType = random.NextBool() ? EthernetType.IpV4 : EthernetType.IpV6;
Layer ipLayer = (ethernetType == EthernetType.IpV4 ? (Layer)ipV4Layer : ipV6Layer);
UdpLayer udpLayer = random.NextUdpLayer();
udpLayer.Checksum = null;
PayloadLayer payloadLayer = random.NextPayloadLayer(random.Next(60000));
Packet packet = PacketBuilder.Build(DateTime.Now, ethernetLayer, ipLayer, udpLayer, payloadLayer);
Assert.IsTrue(packet.IsValid, "IsValid");
// Ethernet
ethernetLayer.EtherType = ethernetType;
Assert.AreEqual(ethernetLayer, packet.Ethernet.ExtractLayer(), "Ethernet Layer");
ethernetLayer.EtherType = EthernetType.None;
// Ip
if (ipLayer == ipV4Layer)
{
// IpV4.
ipV4Layer.Protocol = IpV4Protocol.Udp;
ipV4Layer.HeaderChecksum = ((IpV4Layer)packet.Ethernet.IpV4.ExtractLayer()).HeaderChecksum;
Assert.AreEqual(ipV4Layer, packet.Ethernet.IpV4.ExtractLayer(), "IP Layer");
ipV4Layer.HeaderChecksum = null;
}
else
{
// IpV6.
Assert.AreEqual(ipV6Layer, packet.Ethernet.IpV6.ExtractLayer(), "IP Layer");
}
// UDP
udpLayer.Checksum = packet.Ethernet.Ip.Udp.Checksum;
Assert.AreEqual(udpLayer, packet.Ethernet.Ip.Udp.ExtractLayer(), "UDP Layer");
Assert.AreEqual(UdpDatagram.HeaderLength + payloadLayer.Length, packet.Ethernet.Ip.Udp.TotalLength, "Total Length");
Assert.IsTrue(!udpLayer.CalculateChecksum && packet.Ethernet.Ip.Udp.Checksum == 0 ||
udpLayer.CalculateChecksum && packet.Ethernet.Ip.IsTransportChecksumCorrect, "IsTransportChecksumCorrect");
Assert.IsTrue(packet.Ethernet.Ip.Udp.IsChecksumOptional, "IsChecksumOptional");
Assert.AreEqual(payloadLayer.Data, packet.Ethernet.Ip.Udp.Payload, "Payload");
}
}
示例4: RandomDnsTest
public void RandomDnsTest()
{
EthernetLayer ethernetLayer = new EthernetLayer
{
Source = new MacAddress("00:01:02:03:04:05"),
Destination = new MacAddress("A0:A1:A2:A3:A4:A5")
};
int seed = new Random().Next();
Console.WriteLine("Seed: " + seed);
Random random = new Random(seed);
for (int i = 0; i != 1000; ++i)
{
IpV4Layer ipV4Layer = random.NextIpV4Layer(null);
ipV4Layer.HeaderChecksum = null;
Layer ipLayer = random.NextBool() ? (Layer)ipV4Layer : random.NextIpV6Layer(true);
UdpLayer udpLayer = random.NextUdpLayer();
udpLayer.Checksum = null;
DnsLayer dnsLayer;
do
{
dnsLayer = random.NextDnsLayer();
} while (dnsLayer.Length > 65000 - ipLayer.Length);
Packet packet = PacketBuilder.Build(DateTime.Now, ethernetLayer, ipLayer, udpLayer, dnsLayer);
Assert.IsTrue(packet.IsValid, "IsValid");
// DNS
DnsLayer actualLayer = (DnsLayer)packet.Ethernet.Ip.Udp.Dns.ExtractLayer();
Assert.AreEqual(dnsLayer, actualLayer, "DNS Layer");
Assert.IsTrue(packet.Ethernet.Ip.Udp.Dns.IsValid);
DnsDataResourceRecord opt = packet.Ethernet.Ip.Udp.Dns.Additionals.FirstOrDefault(additional => additional.DnsType == DnsType.Opt);
Assert.AreEqual(opt, packet.Ethernet.Ip.Udp.Dns.OptionsRecord);
foreach (var record in packet.Ethernet.Ip.Udp.Dns.ResourceRecords)
{
Assert.IsTrue(record.Equals(record));
Assert.IsTrue(record.DomainName.Equals((object)record.DomainName));
Assert.IsTrue(record.DomainName.Equals((object)record.DomainName));
Assert.AreEqual(record.GetHashCode(), record.GetHashCode());
}
foreach (var record in packet.Ethernet.Ip.Udp.Dns.DataResourceRecords)
{
MoreAssert.IsBiggerOrEqual(9, record.ToString().Length);
Assert.IsTrue(record.Equals((object)record));
Assert.IsInstanceOfType(record.Data, DnsResourceData.GetDnsResourceDataType(record.DnsType) ?? typeof(DnsResourceDataAnything));
Assert.IsTrue(record.DomainName.Equals((object)record.DomainName));
Assert.IsFalse(record.Data.Equals(null));
}
}
}
示例5: Island
public Island(Vector2 location, Random random)
{
Location = location;
Effects = random.NextBool() ? SpriteEffects.None : SpriteEffects.FlipHorizontally;
Texture = IslandsCurses.Textures["island"];
Ratio = new Vector2(random.NextRatio(60) * (IslandSpace.X / Texture.Width), random.NextRatio(60) * (IslandSpace.Y / Texture.Width));
Status = Status.Normal;
}
示例6: CheckRandomDataCore
private void CheckRandomDataCore(Random r1, Random r2, int capacity)
{
BitVector d = BitVector.Create(capacity);
Assert.Equal(capacity, d.Capacity);
for (int i1 = 0; i1 < capacity; i1++)
d[i1] = r1.NextBool();
Assert.Equal(capacity, d.Capacity);
for (int i2 = 0; i2 < capacity; i2++)
Assert.Equal(d[i2], r2.NextBool());
}
示例7: Merge8BoolRandomTest
public void Merge8BoolRandomTest()
{
Random random = new Random();
for (int i = 0; i != 10; ++i)
{
byte expectedResult = 0;
bool[] input = new bool[8];
for (int bit = 0; bit != 8; ++bit)
{
bool bitValue = random.NextBool();
input[bit] = bitValue;
expectedResult <<= 1;
if (bitValue)
++expectedResult;
}
Assert.AreEqual(expectedResult, BitSequence.Merge(input[0], input[1], input[2], input[3], input[4], input[5], input[6], input[7]));
}
}
示例8: GetSelectCommandForMultipleRows
/// <summary>
/// Returns a SELECT command that retrieves data from a table
/// </summary>
private String GetSelectCommandForMultipleRows(Random rnd, DbCommand com, TableMetadata inputTable, bool isXml)
{
int rowcount = rnd.Next(Depth);
StringBuilder cmdText = new StringBuilder();
cmdText.Append("SELECT TOP ");
cmdText.Append(rowcount); //Jonfo added this to prevent table scan of 75k row tables
cmdText.Append(" PrimaryKey");
List<TableColumn> columns = inputTable.Columns;
int colindex = rnd.Next(0, columns.Count);
for (int i = 0; i <= colindex; i++)
{
if (columns[i].ColumnName == "PrimaryKey") continue;
cmdText.Append(", ");
cmdText.Append(columns[i].ColumnName);
}
cmdText.Append(" FROM \"");
cmdText.Append(inputTable.TableName);
cmdText.Append("\" WITH(NOLOCK) WHERE PrimaryKey ");
// We randomly pick an operator from '>' or '=' to allow for randomization
// of possible rows returned by this query. This approach *may* help
// in reducing the likelihood of multiple threads accessing same rows.
// If multiple threads access same rows, there may be locking issues
// which may be avoided because of this randomization.
string op = rnd.NextBool() ? ">" : "=";
cmdText.Append(op).Append(" ");
string pName = GetParameterName("P0");
cmdText.Append(pName);
DbParameter param = DbFactory.CreateParameter();
param.ParameterName = pName;
param.Value = GetRandomPK(rnd, inputTable);
param.DbType = DbType.Int32;
com.Parameters.Add(param);
return cmdText.ToString();
}
示例9: CreateRandomIpV4Payload
private static void CreateRandomIpV4Payload(Random random, IpV4Layer ipV4Layer, List<ILayer> layers)
{
if (random.NextBool(20))
{
// Finish with payload.
PayloadLayer payloadLayer = random.NextPayloadLayer(random.Next(100));
layers.Add(payloadLayer);
return;
}
ipV4Layer.Protocol = null;
if (random.NextBool())
ipV4Layer.Fragmentation = IpV4Fragmentation.None;
switch (random.Next(0, 9))
{
case 0: // IpV4.
case 1:
IpV4Layer innerIpV4Layer = random.NextIpV4Layer();
layers.Add(innerIpV4Layer);
CreateRandomIpV4Payload(random, innerIpV4Layer, layers);
return;
case 2: // Igmp.
layers.Add(random.NextIgmpLayer());
return;
case 3: // Icmp.
IcmpLayer icmpLayer = random.NextIcmpLayer();
layers.Add(icmpLayer);
layers.AddRange(random.NextIcmpPayloadLayers(icmpLayer));
return;
case 4: // Gre.
GreLayer greLayer = random.NextGreLayer();
layers.Add(greLayer);
CreateRandomEthernetPayload(random, greLayer, layers);
return;
case 5: // Udp.
case 6:
UdpLayer udpLayer = random.NextUdpLayer();
layers.Add(udpLayer);
CreateRandomUdpPayload(random, udpLayer, layers);
return;
case 7: // Tcp.
case 8:
TcpLayer tcpLayer = random.NextTcpLayer();
layers.Add(tcpLayer);
CreateRandomTcpPayload(random, tcpLayer, layers);
return;
default:
throw new InvalidOperationException("Invalid value.");
}
}
示例10: CheckIsTrue
public void CheckIsTrue()
{
var r1 = new Random(seed);
for (int capacity = 0; capacity < maxBits; capacity++)
{
BitVector b = BitVector.Create(capacity);
for (int i = 0; i < capacity; i++)
{
b[i] = r1.NextBool();
}
var index = 0;
foreach (var word in b.Words())
{
for (var i = 0; i < BitVector.BitsPerWord; i++)
{
if (index >= capacity)
{
break;
}
Assert.Equal(b[index], BitVector.IsTrue(word, index));
index++;
}
}
}
}
示例11: ShouldModifySession
public bool ShouldModifySession(Random rnd)
{
// 33% of the time, we want to modify the user session on the server
return rnd.NextBool(.33);
}
示例12: CreateRandomEthernetPayload
private static void CreateRandomEthernetPayload(Random random, EthernetBaseLayer ethernetBaseLayer, List<ILayer> layers)
{
if (random.NextBool(20))
{
// Finish with payload.
PayloadLayer payloadLayer = random.NextPayloadLayer(random.Next(100));
layers.Add(payloadLayer);
return;
}
ethernetBaseLayer.EtherType = EthernetType.None;
switch (random.NextInt(0, 7))
{
case 0: // VLanTaggedFrame.
case 1:
VLanTaggedFrameLayer vLanTaggedFrameLayer = random.NextVLanTaggedFrameLayer();
layers.Add(vLanTaggedFrameLayer);
CreateRandomEthernetPayload(random, vLanTaggedFrameLayer, layers);
return;
case 2: // ARP.
EthernetLayer ethernetLayer = (ethernetBaseLayer as EthernetLayer);
if (ethernetLayer != null)
ethernetLayer.Destination = MacAddress.Zero;
layers.Add(random.NextArpLayer());
return;
case 3: // IPv4.
case 4:
IpV4Layer ipV4Layer = random.NextIpV4Layer();
layers.Add(ipV4Layer);
CreateRandomIpPayload(random, ipV4Layer, layers);
return;
case 5: // IPv6
case 6:
IpV6Layer ipV6Layer = random.NextIpV6Layer(random.NextBool(20));
layers.Add(ipV6Layer);
CreateRandomIpPayload(random, ipV6Layer, layers);
return;
default:
throw new InvalidOperationException("Invalid value.");
}
}
示例13: RandomGreTest
public void RandomGreTest()
{
EthernetLayer ethernetLayer = new EthernetLayer
{
Source = new MacAddress("00:01:02:03:04:05"),
Destination = new MacAddress("A0:A1:A2:A3:A4:A5")
};
int seed = new Random().Next();
Console.WriteLine("Seed: " + seed);
Random random = new Random(seed);
for (int i = 0; i != 200; ++i)
{
IpV4Layer ipV4Layer = random.NextIpV4Layer(null);
ipV4Layer.HeaderChecksum = null;
Layer ipLayer = random.NextBool() ? (Layer)ipV4Layer : random.NextIpV6Layer(IpV4Protocol.Gre, false);
GreLayer greLayer = random.NextGreLayer();
PayloadLayer payloadLayer = random.NextPayloadLayer(random.Next(100));
PacketBuilder packetBuilder = new PacketBuilder(ethernetLayer, ipLayer, greLayer, payloadLayer);
Packet packet = packetBuilder.Build(DateTime.Now);
if (greLayer.Checksum == null &&
!new[] { EthernetType.IpV4, EthernetType.IpV6, EthernetType.Arp, EthernetType.VLanTaggedFrame }.Contains(packet.Ethernet.Ip.Gre.ProtocolType))
{
Assert.IsTrue(packet.IsValid, "IsValid, ProtocolType=" + packet.Ethernet.Ip.Gre.ProtocolType);
}
// Ethernet
ethernetLayer.EtherType = ipLayer == ipV4Layer ? EthernetType.IpV4 : EthernetType.IpV6;
Assert.AreEqual(ethernetLayer, packet.Ethernet.ExtractLayer(), "Ethernet Layer");
ethernetLayer.EtherType = EthernetType.None;
// IP.
if (ipLayer == ipV4Layer)
{
// IPv4.
ipV4Layer.Protocol = IpV4Protocol.Gre;
ipV4Layer.HeaderChecksum = ((IpV4Layer)packet.Ethernet.Ip.ExtractLayer()).HeaderChecksum;
Assert.AreEqual(ipV4Layer, packet.Ethernet.Ip.ExtractLayer());
ipV4Layer.HeaderChecksum = null;
Assert.AreEqual(ipV4Layer.Length, packet.Ethernet.IpV4.HeaderLength);
Assert.IsTrue(packet.Ethernet.IpV4.IsHeaderChecksumCorrect);
Assert.AreEqual(ipV4Layer.Length + greLayer.Length + payloadLayer.Length,
packet.Ethernet.Ip.TotalLength);
Assert.AreEqual(IpV4Datagram.DefaultVersion, packet.Ethernet.Ip.Version);
}
else
{
// IPv6.
Assert.AreEqual(ipLayer, packet.Ethernet.Ip.ExtractLayer());
}
// GRE
GreDatagram actualGre = packet.Ethernet.Ip.Gre;
GreLayer actualGreLayer = (GreLayer)actualGre.ExtractLayer();
if (greLayer.ChecksumPresent && greLayer.Checksum == null)
{
Assert.IsTrue(actualGre.IsChecksumCorrect);
greLayer.Checksum = actualGre.Checksum;
}
Assert.AreEqual(greLayer, actualGreLayer, "Layer");
if (actualGreLayer.Key != null)
actualGreLayer.SetKey(actualGreLayer.KeyPayloadLength.Value, actualGreLayer.KeyCallId.Value);
else
{
Assert.IsNull(actualGreLayer.KeyPayloadLength);
Assert.IsNull(actualGreLayer.KeyCallId);
}
Assert.AreEqual(greLayer, actualGreLayer, "Layer");
if (actualGre.KeyPresent)
{
Assert.AreEqual(greLayer.KeyPayloadLength, actualGre.KeyPayloadLength, "KeyPayloadLength");
Assert.AreEqual(greLayer.KeyCallId, actualGre.KeyCallId, "KeyCallId");
}
Assert.AreNotEqual(random.NextGreLayer(), actualGreLayer, "Not Layer");
Assert.AreEqual(greLayer.Length, actualGre.HeaderLength);
Assert.IsTrue(actualGre.KeyPresent ^ (greLayer.Key == null));
MoreAssert.IsSmaller(8, actualGre.RecursionControl);
MoreAssert.IsSmaller(32, actualGre.FutureUseBits);
Assert.IsTrue(actualGre.RoutingPresent ^ (greLayer.Routing == null && greLayer.RoutingOffset == null));
Assert.IsTrue(actualGre.SequenceNumberPresent ^ (greLayer.SequenceNumber == null));
Assert.IsTrue(!actualGre.StrictSourceRoute || actualGre.RoutingPresent);
if (actualGre.RoutingPresent)
{
Assert.IsNotNull(actualGre.ActiveSourceRouteEntryIndex);
if (actualGre.ActiveSourceRouteEntryIndex < actualGre.Routing.Count)
Assert.IsNotNull(actualGre.ActiveSourceRouteEntry);
foreach (GreSourceRouteEntry entry in actualGre.Routing)
{
Assert.AreNotEqual(entry, 2);
Assert.AreEqual(entry.GetHashCode(), entry.GetHashCode());
switch (entry.AddressFamily)
{
case GreSourceRouteEntryAddressFamily.AsSourceRoute:
GreSourceRouteEntryAs asEntry = (GreSourceRouteEntryAs)entry;
MoreAssert.IsInRange(0, asEntry.AsNumbers.Count, asEntry.NextAsNumberIndex);
//.........这里部分代码省略.........
示例14: InvalidGreTest
public void InvalidGreTest()
{
EthernetLayer ethernetLayer = new EthernetLayer
{
Source = new MacAddress("00:01:02:03:04:05"),
Destination = new MacAddress("A0:A1:A2:A3:A4:A5")
};
int seed = new Random().Next();
Console.WriteLine("Seed: " + seed);
Random random = new Random(seed);
for (int i = 0; i != 100; ++i)
{
IpV4Layer ipV4Layer = random.NextIpV4Layer(IpV4Protocol.Gre);
ipV4Layer.HeaderChecksum = null;
Layer ipLayer = random.NextBool() ? (Layer)ipV4Layer : random.NextIpV6Layer(IpV4Protocol.Gre, false);
GreLayer greLayer = random.NextGreLayer();
greLayer.Checksum = null;
greLayer.Routing = new List<GreSourceRouteEntry>
{
new GreSourceRouteEntryAs(new List<ushort> {123}.AsReadOnly(), 0),
new GreSourceRouteEntryIp(new List<IpV4Address> {random.NextIpV4Address()}.AsReadOnly(),
0)
}.AsReadOnly();
PacketBuilder packetBuilder = new PacketBuilder(ethernetLayer, ipLayer, greLayer);
Packet packet = packetBuilder.Build(DateTime.Now);
Assert.IsTrue(packet.IsValid ||
new[] { EthernetType.IpV4, EthernetType.IpV6, EthernetType.Arp, EthernetType.VLanTaggedFrame }.Contains(greLayer.ProtocolType),
"IsValid. ProtoclType=" + greLayer.ProtocolType);
GreDatagram gre = packet.Ethernet.Ip.Gre;
// Remove a byte from routing
Datagram newIpPayload = new Datagram(gre.Take(gre.Length - 1).ToArray());
packetBuilder = new PacketBuilder(ethernetLayer, ipLayer, new PayloadLayer {Data = newIpPayload});
packet = packetBuilder.Build(DateTime.Now);
Assert.IsNull(packet.Ethernet.Ip.Gre.Payload);
Assert.IsFalse(packet.IsValid);
// SreLength is too big
byte[] buffer = gre.ToArray();
buffer[buffer.Length - 1] = 200;
newIpPayload = new Datagram(buffer);
packetBuilder = new PacketBuilder(ethernetLayer, ipLayer, new PayloadLayer {Data = newIpPayload});
packet = packetBuilder.Build(DateTime.Now);
Assert.IsFalse(packet.IsValid);
// PayloadOffset is too big
buffer = gre.ToArray();
buffer[gre.Length - 10] = 100;
newIpPayload = new Datagram(buffer);
packetBuilder = new PacketBuilder(ethernetLayer, ipLayer, new PayloadLayer {Data = newIpPayload});
packet = packetBuilder.Build(DateTime.Now);
Assert.IsFalse(packet.IsValid);
// PayloadOffset isn't aligned to ip
buffer = gre.ToArray();
buffer[gre.Length - 10] = 3;
newIpPayload = new Datagram(buffer);
packetBuilder = new PacketBuilder(ethernetLayer, ipLayer, new PayloadLayer {Data = newIpPayload});
packet = packetBuilder.Build(DateTime.Now);
Assert.IsFalse(packet.IsValid);
// PayloadOffset isn't aligned to as
buffer = gre.ToArray();
buffer[gre.Length - 16] = 1;
newIpPayload = new Datagram(buffer);
packetBuilder = new PacketBuilder(ethernetLayer, ipLayer, new PayloadLayer {Data = newIpPayload});
packet = packetBuilder.Build(DateTime.Now);
Assert.IsFalse(packet.IsValid);
}
}
示例15: CheckUnion
private void CheckUnion(int capacity, Random r)
{
BitArray b1 = BitArray.Empty, b2 = BitArray.Empty;
b1.EnsureCapacity(capacity);
b2.EnsureCapacity(capacity);
bool[] a1 = new bool[capacity], a2 = new bool[capacity];
for (int i = 0; i < capacity; i++)
{
b1[i] = a1[i] = r.NextBool();
b2[i] = a2[i] = r.NextBool();
}
b1.UnionWith(b2);
for (int i = 0; i < capacity; i++)
{
a1[i] |= a2[i];
}
for (int i = 0; i < capacity; i++)
{
Assert.Equal(a1[i], b1[i]);
}
}