我希望生成一个数字序列,其中每个数字都在70和100之间,序列中将有x个数字,它将给出y的和平均值。这个算法看起来会是什么样子?
发布于 2012-06-22 09:30:08
Python示例
import random
import time
x = 10
total = 0
avg = 0
random.seed(time.time())
for x in range(10):
total += random.randint(70,100)
avg = total /x
print "total: ", total
print "avg: ", avg
发布于 2012-06-22 09:39:52
Random r = new Random();
List<int> l = new List<int>();
Console.Write("Please enter amount of randoms ");
int num = (int)Console.Read();
for (int i = 0; i < num; i++)
{
l.Add(r.Next(0, 30) + 70);
}
//calculate avg
int sum = 0;
foreach (int i in l)
{
sum += i;
}
Console.Write("The average of " + num + " random numbers is " + (sum / num));
//to stop the program from closing automatically
Console.ReadKey();
https://stackoverflow.com/questions/11149051
复制相似问题