Data Random comprese da x a y

using System;

using System.Collections;

public class MyClass

{

public static void Main()

{

RandomEx rg=new RandomEx();

DateTime di=new System.DateTime(2005,4,1);

DateTime df=new System.DateTime(2005,5,1);

for (int i=0;i<20;i++)

{

WL("Riga \t {0} \t {1}",i,rg.NextDateTime(di,df));

}

RL();

}

#region Helper methods

private static void WL(object text, params object[] args)

{

Console.WriteLine(text.ToString(), args);

}

private static void RL()

{

Console.ReadLine();

}

private static void Break()

{

System.Diagnostics.Debugger.Break();

}

#endregion

public class RandomEx:System.Random

{

public RandomEx():base()

{

}

public RandomEx(int Seed):base(Seed)

{

}

public virtual long Next(long minValue, long maxValue)

{

if (minValue > maxValue)

{

throw new ArgumentOutOfRangeException("minValue","Parametri non validi" );

}

long num1 = maxValue - minValue;

if (num1 < 0)

{

long num2 = maxValue - minValue;

return (((long) ((long) (this.Sample() * num2))) + minValue);

}

return (((long) (this.Sample() * num1)) + minValue);

}

 

public virtual System.DateTime NextDateTime()

{

return NextDateTime(new System.DateTime(1970,01,01),new System.DateTime(2050,12,31));

}

public virtual System.DateTime NextDateTime(System.DateTime maxValue)

{

return NextDateTime(new System.DateTime(1970,01,01),maxValue);

}

public virtual System.DateTime NextDateTime(System.DateTime minValue, System.DateTime maxValue)

{

long minTicks=minValue.Ticks;

long maxTicks=maxValue.Ticks;

if (minTicks>maxTicks)

{

throw new ArgumentOutOfRangeException("minValue","Parametri non validi" );

}

return new System.DateTime(Next(minTicks,maxTicks));

}

}

}

Print | posted on sabato 30 aprile 2005 19:16