• Write a program that outputs 6 random number in the range of 1-45
• Hint: % operator
• Allow duplicate numbers
例子)
You can win the first prize at lotto !! 19 27 23 15 34 40
static void Main(string[] args)
{
Random objRandom = new Random();
string nums = null;
for (int i = 0; i < 6; i++)
{
nums += objRandom.Next(1, 45) + " ";
}
Console.WriteLine(nums);
Console.ReadKey();
}
int main()
{
char *s;
int i=0;
s="You can win the first prize at lotto !!";
printf("%s\n",s);
for(i=0;i<6;i++)
{
printf("%d\t",rand()%45+1);
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
srand((unsigned)time(NULL));
printf("You can win the first prize at lotto !! ");
int i;
for (i = 0; i < 6; i++)
{
int a = 1; int b = 45; //为了让你看清楚下面怎么来的,这里定义a b两个变量,实际上你可以直接写死。
int r = (rand() % (b - a + 1)) + a; // [a, b]
printf("%d ", r);
}
return 0;
}
在Xcode中
#import
int main(int argc, const char * argv[]) {
printf("You can win the first prize at lotto !! \n ");
for (int i = 0; i < 6; i++)
{
// 求随机数公式 [a b] int number = arc4random() % (a - b + 1) + a
int a = arc4random() % (45 - 1 + 1) + 1;
printf("%d ", a);
}
return 0;
}