kromm77 icon

[C#] Generazione numero random automatico

kromm77 | PRO | 12/21/22 04:22:26 PM UTC | 0 ⭐ | 371 👁️ | Never ⏰ | []
text |

2.44 KB

|

None

|

0 👍

/

0 👎

using System;
 public class Program
{
	public static void Main()
	{
		Console.WriteLine("Hello World");
		var rnd = new ExponentialRandom();
 		var len = 16;
		var l1 = len / 2;
		var l2 = len - l1;
		Console.WriteLine(" L1 "+l1+ " L2 "+l2 );
		var nu = CreateANumber(rnd, l1 , l2 );
 		Console.WriteLine("0123456789abcdef" );
		Console.WriteLine("ea2345c7891b6d0f" );
		Console.WriteLine("0123012301230123" );
		Console.WriteLine(nu);
		var a = 10;
		var b = 11;
		var c = 12;
		var d = 13;
		var e = 14;
		var f = 15;
		var pos = new int[16] {
		e,
			a,
			2,
			3,
 			4,
			5,
			c,
			7,
 			8,			
			9,
			1,
			b,
 			6,			 
			d,
			0,
			f	
		};
 		var nun = "0123456789123456";
		var newnu = new System.Text.StringBuilder();
		for(var i = 0; i <16; i++ ) {
			newnu.Append(nun.Substring(pos[i],1));
		}
		Console.WriteLine(newnu);
 	}
	public static long CreateANumber(ExponentialRandom rnd, int l1, int l2)
        {
            var first = rnd.GetRandom(l1);
            var second = rnd.GetRandom(l2);
		Console.WriteLine(" first 01234567 second 01234567");
		Console.WriteLine(" first "+first+ " second "+second );
		Console.WriteLine(" L1 "+l1+ " L2 "+l2 );
            return (long)(first * Math.Pow(10, l2) + second);
        }
  }
  public class MyUintRandom
    {
        private static System.Random _rnd = new System.Random();
         public MyUintRandom(System.Random rnd)
        {
            _rnd = rnd;
        }
         public uint GetRandom(uint max)
        {
            var middle = (int) max / 2;
            var first = _rnd.Next(middle);
            var second = _rnd.Next(middle);
            return (uint) (first + second);
        }
    }
public class ExponentialRandom
    {
        private static System.Random _rnd;
        private static MyUintRandom _myRnd;
         public ExponentialRandom()
            : this(new System.Random())
        {
        }
         public ExponentialRandom(System.Random rnd)
        {
            _rnd = rnd;
            _myRnd = new MyUintRandom(_rnd);
        }
         public uint GetRandom(uint exp)
        {
            var maxValue = (uint) Math.Pow(10, exp);
            return _myRnd.GetRandom(maxValue);
        }
         public int GetRandom(int exp)
        {
            var maxValue = (int) Math.Pow(10, exp);
            return _rnd.Next(maxValue);
        }
    }

Comments