本文整理汇总了C#中System.Random.NextInt方法的典型用法代码示例。如果您正苦于以下问题:C# Random.NextInt方法的具体用法?C# Random.NextInt怎么用?C# Random.NextInt使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Random
的用法示例。
在下文中一共展示了Random.NextInt方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: RandomIpV4Test
public void RandomIpV4Test()
{
MacAddress ethernetSource = new MacAddress("00:01:02:03:04:05");
MacAddress ethernetDestination = new MacAddress("A0:A1:A2:A3:A4:A5");
const EthernetType EthernetType = EthernetType.IpV4;
EthernetLayer ethernetLayer = new EthernetLayer
{
Source = ethernetSource,
Destination = ethernetDestination,
EtherType = EthernetType
};
Random random = new Random();
for (int i = 0; i != 1000; ++i)
{
IpV4Layer ipV4Layer = random.NextIpV4Layer();
ipV4Layer.HeaderChecksum = null;
PayloadLayer payloadLayer = random.NextPayloadLayer(random.NextInt(0, 50 * 1024));
Packet packet = PacketBuilder.Build(DateTime.Now, ethernetLayer, ipV4Layer, payloadLayer);
Assert.IsTrue(ipV4Layer.Protocol == IpV4Protocol.Udp ||
ipV4Layer.Protocol == IpV4Protocol.Tcp ||
ipV4Layer.Protocol == IpV4Protocol.InternetGroupManagementProtocol ||
ipV4Layer.Protocol == IpV4Protocol.InternetControlMessageProtocol ||
ipV4Layer.Protocol == IpV4Protocol.Gre ||
packet.IsValid, "IsValid (" + ipV4Layer.Protocol + ")");
// Ethernet
Assert.AreEqual(packet.Length - EthernetDatagram.HeaderLengthValue, packet.Ethernet.PayloadLength, "PayloadLength");
Assert.AreEqual(ethernetLayer, packet.Ethernet.ExtractLayer(), "Ethernet Layer");
// IpV4
ipV4Layer.HeaderChecksum = ((IpV4Layer)packet.Ethernet.IpV4.ExtractLayer()).HeaderChecksum;
Assert.AreEqual(ipV4Layer, packet.Ethernet.IpV4.ExtractLayer(), "IP Layer");
Assert.AreEqual(ipV4Layer.Destination, packet.Ethernet.IpV4.Destination, "Destination");
Assert.AreNotEqual(ipV4Layer, null);
Assert.AreNotEqual(ipV4Layer, new PayloadLayer());
Assert.IsNotNull(ipV4Layer.ToString());
Assert.AreEqual(IpV4Datagram.HeaderMinimumLength + ipV4Layer.Options.BytesLength, packet.Ethernet.IpV4.HeaderLength, "IP HeaderLength");
Assert.AreEqual(packet.Length - EthernetDatagram.HeaderLengthValue, packet.Ethernet.IpV4.TotalLength, "IP TotalLength");
Assert.AreNotEqual(2, packet.Ethernet.IpV4.Fragmentation, "IP Fragmentation");
Assert.IsTrue(ipV4Layer.Fragmentation == packet.Ethernet.IpV4.Fragmentation, "IP Fragmentation");
Assert.IsFalse(ipV4Layer.Fragmentation != packet.Ethernet.IpV4.Fragmentation, "IP Fragmentation");
Assert.IsFalse(ipV4Layer.Fragmentation.Equals(0), "IP Fragmentation");
if (ipV4Layer.Fragmentation.Offset != 0)
Assert.AreNotEqual(ipV4Layer.Fragmentation, IpV4Fragmentation.None, "IP Fragmentation");
Assert.AreEqual(ipV4Layer.Fragmentation.GetHashCode(), packet.Ethernet.IpV4.Fragmentation.GetHashCode(), "IP Fragmentation");
Assert.AreEqual(ipV4Layer.Fragmentation.Options, packet.Ethernet.IpV4.Fragmentation.Options, "IP Fragmentation");
Assert.AreEqual(ipV4Layer.Fragmentation.Offset, packet.Ethernet.IpV4.Fragmentation.Offset, "IP Fragmentation");
if (ipV4Layer.Fragmentation.Equals(IpV4Fragmentation.None))
{
Assert.AreEqual(IpV4FragmentationOptions.None, packet.Ethernet.IpV4.Fragmentation.Options, "IP Fragmentation");
Assert.AreEqual(0, packet.Ethernet.IpV4.Fragmentation.Offset, "IP Fragmentation");
}
Assert.AreEqual(true, packet.Ethernet.IpV4.IsHeaderChecksumCorrect, "IP HeaderChecksumCorrect");
Assert.AreNotEqual(null, packet.Ethernet.IpV4.Options, "IP Options");
Assert.AreNotEqual(packet.Ethernet.IpV4.Options, null, "IP Options");
Assert.AreNotEqual(new IpV4Options(new IpV4OptionUnknown(0, new byte[35])), packet.Ethernet.IpV4.Options, "IP Options");
Assert.AreEqual(ipV4Layer.Options.GetHashCode(), packet.Ethernet.IpV4.Options.GetHashCode(), "IP Options HashCode");
Assert.IsNotNull(packet.Ethernet.IpV4.Options.ToString());
for (int optionIndex = 0; optionIndex != ipV4Layer.Options.Count; ++optionIndex)
{
IpV4Option option = ipV4Layer.Options[optionIndex];
Assert.AreEqual(option, packet.Ethernet.IpV4.Options[optionIndex]);
Assert.IsFalse(option.Equals(null));
}
if (packet.Ethernet.IpV4.Protocol == IpV4Protocol.Tcp)
Assert.IsInstanceOfType(packet.Ethernet.IpV4.Transport, typeof(TcpDatagram));
else if (packet.Ethernet.IpV4.Protocol == IpV4Protocol.Udp)
Assert.IsInstanceOfType(packet.Ethernet.IpV4.Transport, typeof(UdpDatagram));
else
Assert.IsNull(packet.Ethernet.IpV4.Transport);
Assert.AreEqual(payloadLayer.Data, packet.Ethernet.IpV4.Payload, "IP Payload");
}
}
示例2: RandomIpV6Test
public void RandomIpV6Test()
{
MacAddress ethernetSource = new MacAddress("00:01:02:03:04:05");
MacAddress ethernetDestination = new MacAddress("A0:A1:A2:A3:A4:A5");
const EthernetType EthernetType = EthernetType.IpV6;
EthernetLayer ethernetLayer = new EthernetLayer
{
Source = ethernetSource,
Destination = ethernetDestination,
EtherType = EthernetType
};
int seed = new Random().Next();
Console.WriteLine("Seed: " + seed);
Random random = new Random(seed);
for (int i = 0; i != 1000; ++i)
{
IpV6Layer ipV6Layer = random.NextIpV6Layer(false);
PayloadLayer payloadLayer = random.NextPayloadLayer(random.NextInt(0, 50 * 1024));
List<ILayer> layers = new List<ILayer> {ethernetLayer, ipV6Layer};
if (ipV6Layer.ExtensionHeaders.LastHeader != IpV4Protocol.EncapsulatingSecurityPayload)
layers.Add(payloadLayer);
Packet packet = PacketBuilder.Build(DateTime.Now, layers);
Assert.IsTrue(ipV6Layer.LastNextHeader == IpV4Protocol.Ip ||
ipV6Layer.LastNextHeader == IpV4Protocol.IpV6||
ipV6Layer.LastNextHeader == IpV4Protocol.Udp ||
ipV6Layer.LastNextHeader == IpV4Protocol.Tcp ||
ipV6Layer.LastNextHeader == IpV4Protocol.InternetGroupManagementProtocol ||
ipV6Layer.LastNextHeader == IpV4Protocol.InternetControlMessageProtocol ||
ipV6Layer.LastNextHeader == IpV4Protocol.Gre ||
packet.IsValid, string.Format("IsValid ({0}...{1})", ipV6Layer.NextHeader, ipV6Layer.ExtensionHeaders.NextHeader));
// Ethernet
Assert.AreEqual(packet.Length - EthernetDatagram.HeaderLengthValue, packet.Ethernet.PayloadLength, "PayloadLength");
Assert.AreEqual(ethernetLayer, packet.Ethernet.ExtractLayer(), "Ethernet Layer");
// IpV6
Assert.AreEqual(ipV6Layer, packet.Ethernet.IpV6.ExtractLayer(), "IP Layer");
Assert.AreEqual(ipV6Layer.GetHashCode(), packet.Ethernet.IpV6.ExtractLayer().GetHashCode(), "IP Layer");
Assert.AreEqual(string.Format("{0} -> {1} ({2})", ipV6Layer.Source, ipV6Layer.CurrentDestination, ipV6Layer.NextHeader), ipV6Layer.ToString());
IEnumerator extensionHeadersEnumerator = ((IEnumerable)packet.Ethernet.IpV6.ExtensionHeaders).GetEnumerator();
for (int extensionHeaderIndex = 0; extensionHeaderIndex != packet.Ethernet.IpV6.ExtensionHeaders.Headers.Count; ++extensionHeaderIndex)
{
IpV6ExtensionHeader extensionHeader = packet.Ethernet.IpV6.ExtensionHeaders[extensionHeaderIndex];
Assert.IsTrue(extensionHeadersEnumerator.MoveNext());
Assert.AreEqual(extensionHeader, extensionHeadersEnumerator.Current);
IpV6ExtensionHeader layerExtensionheader = ipV6Layer.ExtensionHeaders[extensionHeaderIndex];
Assert.AreEqual(extensionHeader, layerExtensionheader);
Assert.AreEqual(extensionHeader.GetHashCode(), layerExtensionheader.GetHashCode());
IpV6ExtensionHeaderOptions extensionHeaderOptions = extensionHeader as IpV6ExtensionHeaderOptions;
if (extensionHeaderOptions != null)
{
foreach (IpV6Option option in extensionHeaderOptions.Options)
{
switch (option.OptionType)
{
case IpV6OptionType.SimplifiedMulticastForwardingDuplicatePacketDetection:
IpV6OptionSimplifiedMulticastForwardingDuplicatePacketDetection optionSimplifiedMulticastForwardingDuplicatePacketDetection = (IpV6OptionSimplifiedMulticastForwardingDuplicatePacketDetection)option;
Assert.AreEqual(optionSimplifiedMulticastForwardingDuplicatePacketDetection is IpV6OptionSimplifiedMulticastForwardingDuplicatePacketDetectionSequenceHashAssistValue, optionSimplifiedMulticastForwardingDuplicatePacketDetection.HashIndicator);
break;
case IpV6OptionType.QuickStart:
IpV6OptionQuickStart optionQuickStart = (IpV6OptionQuickStart)option;
MoreAssert.IsBiggerOrEqual(0, optionQuickStart.RateKbps);
break;
}
}
}
IpV6ExtensionHeaderMobility extensionHeaderMobility = extensionHeader as IpV6ExtensionHeaderMobility;
if (extensionHeaderMobility != null)
{
Assert.IsFalse(extensionHeaderMobility.Equals(2));
Assert.IsTrue(extensionHeaderMobility.Equals((object)extensionHeader));
Assert.AreEqual(extensionHeaderMobility.MobilityOptions, new IpV6MobilityOptions(extensionHeaderMobility.MobilityOptions).AsEnumerable());
foreach (IpV6MobilityOption option in extensionHeaderMobility.MobilityOptions)
{
switch (option.OptionType)
{
case IpV6MobilityOptionType.BindingIdentifier:
IpV6MobilityOptionBindingIdentifier optionBindingIdentifier = (IpV6MobilityOptionBindingIdentifier)option;
if (optionBindingIdentifier.IpV4CareOfAddress.HasValue)
Assert.AreEqual(optionBindingIdentifier.IpV4CareOfAddress.Value, optionBindingIdentifier.CareOfAddress);
else if (optionBindingIdentifier.IpV6CareOfAddress.HasValue)
Assert.AreEqual(optionBindingIdentifier.IpV6CareOfAddress.Value, optionBindingIdentifier.CareOfAddress);
else
Assert.IsNull(optionBindingIdentifier.CareOfAddress);
break;
case IpV6MobilityOptionType.AccessNetworkIdentifier:
IpV6MobilityOptionAccessNetworkIdentifier optionAccessNetworkIdentifier = (IpV6MobilityOptionAccessNetworkIdentifier)option;
foreach (IpV6AccessNetworkIdentifierSubOption subOption in optionAccessNetworkIdentifier.SubOptions)
{
switch (subOption.OptionType)
{
case IpV6AccessNetworkIdentifierSubOptionType.GeoLocation:
//.........这里部分代码省略.........
示例3: 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.");
}
}
示例4: IpV6MobilityOptionUnknown
public void IpV6MobilityOptionUnknown()
{
Random random = new Random();
DataSegment data = random.NextDataSegment(random.NextInt(0, 100));
IpV6MobilityOptionUnknown option = new IpV6MobilityOptionUnknown((IpV6MobilityOptionType)0xBB, data);
Assert.AreEqual(data, option.Data);
Packet packet = PacketBuilder.Build(
DateTime.Now,
new EthernetLayer(),
new IpV6Layer
{
ExtensionHeaders = new IpV6ExtensionHeaders(
new IpV6ExtensionHeaderMobilityBindingError(IpV4Protocol.Skip, 0, IpV6BindingErrorStatus.UnrecognizedMobilityHeaderTypeValue,
IpV6Address.Zero, new IpV6MobilityOptions(option)))
});
Assert.IsTrue(packet.IsValid);
Assert.AreEqual(option, ((IpV6ExtensionHeaderMobility)packet.Ethernet.IpV6.ExtensionHeaders[0]).MobilityOptions[0]);
}