Example #1
0
 public static Color32 NextColor32(this Random self)
 {
     return(new Color32(
                self.NextByte(),
                self.NextByte(),
                self.NextByte(),
                self.NextByte()
                ));
 }
Example #2
0
        public void TestByte()
        {
            var rnd = new System.Random();

            for (var c = 0; c < count; c++)
            {
                var mem   = new byte[sizeof(Byte) * writeRepeatCount];
                var span1 = new SpanStream(mem);
                var span2 = new SpanStream(mem);
                var a     = new Byte[writeRepeatCount];

                for (var i = 0; i < writeRepeatCount; i++)
                {
                    var n = rnd.NextByte();
                    a[i] = n;
                    span1.Write(n);
                }

                for (var i = 0; i < writeRepeatCount; i++)
                {
                    var r = span2.ReadByte();
                    Assert.Equal(a[i], r);
                }
            }
        }
        public void TestByte()
        {
            var rnd = new System.Random();

            for (var c = 0; c < count; c++)
            {
                var mem   = new byte[sizeof(Byte) * writeRepeatCount];
                var span1 = new Span <byte>(mem);
                var span2 = new ReadOnlySpan <byte>(mem);
                var a     = new Byte[writeRepeatCount];

                for (var i = 0; i < writeRepeatCount; i++)
                {
                    var n = rnd.NextByte();
                    a[i] = n;
                    span1.MoveWrite(n);
                }

                // Ensure span is not zero
                Assert.NotEqual(0, span2.ToArray().Select(b => (int)b).Sum());
                for (var i = 0; i < writeRepeatCount; i++)
                {
                    var r = span2.MoveReadByte();
                    Assert.Equal(a[i], r);
                }
            }
        }
Example #4
0
 public static decimal NextDecimal(this System.Random generator)
 => new decimal(
     generator.NextInt(),
     generator.NextInt(),
     generator.NextInt(),
     generator.NextBool(),
     generator.NextByte()
     );
        //public async Task<string> yield()
        public async Task yield()
        {
            // where else have we done css remoting?

            var r = new Random();

            body.Attribute("style").Value = "background-color: " +
                "rgb(" +
                    r.NextByte() + "," +
                    r.NextByte() + "," +
                    r.NextByte() +
                ")";


            // server side delay. progressbar shows up?
            //await Task.Delay(2500);

            System.Threading.Thread.Sleep(500);

            //return "ok";
        }
Example #6
0
        public void TestUByte()
        {
            var rnd    = new System.Random();
            var mem    = new byte[sizeof(SByte)];
            var span   = new Span <byte>(mem);
            var roSpan = new Span <byte>(mem);

            for (var c = 0; c < count; c++)
            {
                span.Fill(rnd.NextByte());
                Byte a = 0;
                // Pick any number except zero
                while ((a = rnd.NextByte()) == 0)
                {
                }
                span.Write(a);
                // Ensure span is not zero
                Assert.NotEqual(0, span.ToArray().Select(b => (int)b).Sum());
                var r = roSpan.ReadByte();
                Assert.Equal(a, r);
            }
        }
Example #7
0
        public static object Next(this System.Random generator, Type desired)
        {
            switch (Type.GetTypeCode(desired))
            {
            case TypeCode.Boolean:
                return(generator.NextBool());

            case TypeCode.Byte:
                return(generator.NextByte());

            case TypeCode.Char:
                return(generator.NextChar());

            case TypeCode.DateTime:
                return(generator.NextDateTime());

            case TypeCode.Decimal:
                return(generator.NextDecimal());

            case TypeCode.Double:
                return(generator.NextDouble());

            case TypeCode.Int16:
                return(generator.NextShort());

            case TypeCode.Int32:
                return(generator.NextInt());

            case TypeCode.Int64:
                return(generator.NextLong());

            case TypeCode.SByte:
                return(generator.NextSByte());

            case TypeCode.Single:
                return(generator.NextFloat());

            case TypeCode.UInt16:
                return(generator.NextUShort());

            case TypeCode.UInt32:
                return(generator.NextUInt());

            case TypeCode.UInt64:
                return(generator.NextULong());

            default:
                throw new ArgumentOutOfRangeException("Cannot provide a random " + desired);
            }
        }
Example #8
0
        public void DnsOptResourceRecordTest()
        {
            Random random = new Random();
            for (int i = 0; i != 100; ++i)
            {
                DnsDomainName domainName = random.NextDnsDomainName();
                ushort sendersUdpPayloadSize = random.NextUShort();
                byte extendedRcode = random.NextByte();
                DnsOptVersion version = (DnsOptVersion)random.NextByte();
                DnsOptFlags flags = (DnsOptFlags)random.NextUShort();
                DnsResourceDataOptions data = (DnsResourceDataOptions)random.NextDnsResourceData(DnsType.Opt);

                DnsOptResourceRecord record = new DnsOptResourceRecord(domainName, sendersUdpPayloadSize, extendedRcode, version, flags, data);
                
                Assert.AreEqual(domainName, record.DomainName);
                Assert.AreEqual(sendersUdpPayloadSize, record.SendersUdpPayloadSize);
                Assert.AreEqual(extendedRcode, record.ExtendedReturnCode);
                Assert.AreEqual(version, record.Version);
                Assert.AreEqual(flags, record.Flags);
                Assert.AreEqual(data, record.Data);
            }
        }
Example #9
0
 public void IpV4OptionRoutePointedAddressIndexErrorTest()
 {
     Random random = new Random();
     IpV4Option option = new IpV4OptionRecordRoute(random.NextByte(IpV4OptionRoute.PointedAddressIndexMaxValue + 1, byte.MaxValue + 1));
     Assert.IsNotNull(option);
     Assert.Fail();
 }
Example #10
0
 public void IpV4OptionTimestampPointedIndexErrorTest()
 {
     Random random = new Random();
     IpV4Option option = new IpV4OptionTimestampOnly(0, random.NextByte(IpV4OptionTimestamp.PointedIndexMaxValue + 1, byte.MaxValue + 1));
     Assert.IsNotNull(option);
     Assert.Fail();
 }
Example #11
0
 public void IpV4OptionTimestampOverflowErrorTest()
 {
     Random random = new Random();
     IpV4Option option = new IpV4OptionTimestampOnly(random.NextByte(IpV4OptionTimestamp.OverflowMaxValue + 1, byte.MaxValue + 1), 0);
     Assert.IsNotNull(option);
     Assert.Fail();
 }
Example #12
0
 public static sbyte NextSByte(this System.Random generator) => (sbyte)generator.NextByte();
 void ChangeColor( Random rand, Shape shape, TaskState task )
 {
     shape.PreviousState = shape.Clone() as Shape;
     int delta = rand.NextByte( 1, (int)( MaxColorDelta + 1 ) ) * ( rand.Next( 2 ) == 0 ? 1 : -1 );
     switch( rand.Next( 4 ) ) {
         case 0:
             shape.Color =
                 Color.FromArgb(
                     Math.Max( task.ProjectOptions.MinAlpha, Math.Min( 255, shape.Color.A + delta ) ),
                     shape.Color.R, shape.Color.G, shape.Color.B );
             break;
         case 1:
             shape.Color = Color.FromArgb( shape.Color.A, Math.Max( 0, Math.Min( 255, shape.Color.R + delta ) ),
                                           shape.Color.G, shape.Color.B );
             break;
         case 2:
             shape.Color = Color.FromArgb( shape.Color.A, shape.Color.R,
                                           Math.Max( 0, Math.Min( 255, shape.Color.G + delta ) ), shape.Color.B );
             break;
         case 3:
             shape.Color = Color.FromArgb( shape.Color.A, shape.Color.R, shape.Color.G,
                                           Math.Max( 0, Math.Min( 255, shape.Color.B + delta ) ) );
             break;
     }
 }
 public DNA Mutate( Random rand, DNA oldDNA, TaskState task )
 {
     int maxOverlap = task.ProjectOptions.MaxOverlap;
     DNA newDNA = new DNA( oldDNA );
     Shape shape = newDNA.Shapes[rand.Next( newDNA.Shapes.Length )];
     shape.PreviousState = shape.Clone() as Shape;
     shape.Color = Color.FromArgb( rand.Next( task.ProjectOptions.MinAlpha, 256 ), rand.NextByte(),
                                   rand.NextByte(), rand.NextByte() );
     double area, maxArea = MaxPolygonArea * task.ImageWidth * task.ImageHeight;
     do {
         for( int i = 0; i < shape.Points.Length; i++ ) {
             shape.Points[i] = new PointF( rand.NextFloat( -maxOverlap, task.ImageWidth + maxOverlap ),
                                           rand.NextFloat( -maxOverlap, task.ImageHeight + maxOverlap ) );
         }
         area = CalculateArea( shape.Points );
     } while( area > maxArea );
     newDNA.LastMutation = MutationType.ReplaceShape;
     return newDNA;
 }
 static void ChangeColor( Random rand, Shape shape, TaskState task )
 {
     shape.PreviousState = shape.Clone() as Shape;
     switch( rand.Next( 4 ) ) {
         case 0:
             shape.Color = Color.FromArgb( rand.NextByte( task.ProjectOptions.MinAlpha, 256 ), shape.Color.R,
                                           shape.Color.G, shape.Color.B );
             break;
         case 1:
             shape.Color = Color.FromArgb( shape.Color.A, rand.NextByte(), shape.Color.G, shape.Color.B );
             break;
         case 2:
             shape.Color = Color.FromArgb( shape.Color.A, shape.Color.R, rand.NextByte(), shape.Color.B );
             break;
         case 3:
             shape.Color = Color.FromArgb( shape.Color.A, shape.Color.R, shape.Color.G, rand.NextByte() );
             break;
     }
 }
Example #16
0
        void MutateShape( Random rand, DNA dna, Shape shape, TaskState task )
        {
            shape.PreviousState = shape.Clone() as Shape;
            switch( rand.Next( 10 ) ) {
                case 0:
                    shape.Color = Color.FromArgb( (byte)rand.Next( task.ProjectOptions.MinAlpha, 256 ), shape.Color.R,
                                                  shape.Color.G, shape.Color.B );
                    dna.LastMutation = MutationType.ReplaceColor;
                    if( rand.Next( 10 ) == 0 ) {
                        MutateMultiplePoints( shape, rand, dna, task );
                        dna.LastMutation = MutationType.ReplaceShape;
                    }
                    break;

                case 1:
                    shape.Color = Color.FromArgb( shape.Color.A, rand.NextByte(), shape.Color.G, shape.Color.B );
                    dna.LastMutation = MutationType.ReplaceColor;
                    if( rand.Next( 10 ) == 0 ) {
                        MutateMultiplePoints( shape, rand, dna, task );
                        dna.LastMutation = MutationType.ReplaceShape;
                    }
                    break;

                case 2:
                    shape.Color = Color.FromArgb( shape.Color.A, shape.Color.R, rand.NextByte(), shape.Color.B );
                    dna.LastMutation = MutationType.ReplaceColor;
                    if( rand.Next( 10 ) == 0 ) {
                        MutateMultiplePoints( shape, rand, dna, task );
                        dna.LastMutation = MutationType.ReplaceShape;
                    }
                    break;

                case 3:
                    shape.Color = Color.FromArgb( shape.Color.A, shape.Color.R, shape.Color.G, rand.NextByte() );
                    dna.LastMutation = MutationType.ReplaceColor;
                    if( rand.Next( 10 ) == 0 ) {
                        MutateMultiplePoints( shape, rand, dna, task );
                        dna.LastMutation = MutationType.ReplaceShape;
                    }
                    break;

                default:
                    MutateMultiplePoints( shape, rand, dna, task );
                    break;
            }
        }
Example #17
0
        public static Texture2D GetGrainTexture(IGraphicsService graphicsService, int size)
        {
            if (graphicsService == null)
            throw new ArgumentNullException("graphicsService");
              if (size < 1)
            throw new ArgumentOutOfRangeException("size", "size must be greater than 0.");

              var textures = GetNoiseTextures(graphicsService);
              Texture2D grainTexture;
              if (!textures.GrainTextures.TryGetValue(size, out grainTexture) || grainTexture.IsDisposed)
              {
            grainTexture = new Texture2D(graphicsService.GraphicsDevice, size, size, false, SurfaceFormat.Color);
            Color[] data = new Color[size * size];
            var random = new Random(1234567);
            for (int i = 0; i < data.Length; i++)
              data[i] = new Color(random.NextByte(), random.NextByte(), random.NextByte(), random.NextByte());

            grainTexture.SetData(data);

            textures.GrainTextures[size] = grainTexture;
              }

              return grainTexture;
        }