jeudi 16 avril 2015

Why is this implementation of Superincreasing number sequence not generating an elegant sequence?


class SuperIncreasingNumberSet
{
List<BigInteger> list;
//In mathematics, a sequence of positive real numbers
//{s_1, s_2, ...} is called superincreasing if every element of
//the sequence is greater than the sum of all previous elements
//in the sequence.
public SuperIncreasingNumberSet(BigInteger firstNumber)
{
list = new List<BigInteger>();
list.Add(firstNumber);
}
public void Generate(int i)
{
BigInteger b = 0;

for (int j = 0; j < i; j++)
{
BigInteger sum = 0;
foreach(BigInteger bi in list)
{
sum = sum + bi;
}
Random random = new Random();

int r = random.Next(1, 5);

list.Add(sum + r);
}
}
public List<BigInteger> Get()
{
return list;
}
}

class Program
{
static void Main(string[] args)
{
SuperIncreasingNumberSet sins = new SuperIncreasingNumberSet(1);

sins.Generate(10);

List<BigInteger> list = sins.Get();

foreach (BigInteger bi in list)
{
Console.Write(bi.ToString()+", ");
}
}
}


I was expecting something like this: (1,3,6,13,27,52).


But, mine is generating: (1, 5, 10, 20, 40, 80) and every time it is generating the same sequence.


May be the random isn't working properly.


Why?





Aucun commentaire:

Enregistrer un commentaire