任何人对此都有任何想法..... [英] Anyone have any ideas on this one.....
问题描述
我正在尝试编写一个程序,它将在1到50之间生成100个随机数
。使用这些数字,我想生成一个列表,这将是
告诉随机数在1-5,6-10,11-15之间,
16-20,......和46-50。最后,我想打印出结果作为
直方图。我把它写出来并认为它会是这样的。
1-5(11)***********
6-10(8)********
11-15(12)************
16 -20(9)*********
21-25(10)**********
26-30( 11)***********
31-35(7)*******
36-40(8)** ******
41-45(13)*************
46-50(11)*** ********
我正在努力学习如何做到这一点而且我被卡住了。任何建议都会有帮助。
帮助。甚至一些可能的教程也会有所帮助。
-
使用 http://www.talkaboutprogramming.com/group/comp.lang.c/
更多信息在< a rel =nofollowhref =http://www.talkaboutprogramming.com/faq.htmltarget =_ blank> http://www.talkaboutprogramming.com/faq.html
I am trying to write a program that will generate 100 random numbers
between 1 and 50. Using these numbers, I want to generate a list that will
tell the number of random numbers that fell between 1-5, 6-10, 11-15,
16-20, ... , and 46-50. Finally,I would like to print out the results as a
histogram. I have written it out and would think it would look like this.
1-5 (11) ***********
6-10 (8) ********
11-15 (12) ************
16-20 (9) *********
21-25 (10) **********
26-30 (11) ***********
31-35 (7) *******
36-40 (8) ********
41-45 (13) *************
46-50 (11) ***********
I am trying to learn how to do this and I am stuck. Any suggestions would
help. Even some possible tutorials to chek out would help.
--
Message posted using http://www.talkaboutprogramming.com/group/comp.lang.c/
More information at http://www.talkaboutprogramming.com/faq.html
推荐答案
" joebenjamin" < gu ********* @ hotmail.comwrites:
"joebenjamin" <gu*********@hotmail.comwrites:
我正在尝试编写一个可生成100个随机数的程序
在1到50之间。使用这些数字,我想生成一个列表,它将
告知1-5,6-10,11-15之间的随机数,
16-20,......和46-50。最后,我想打印出结果作为
直方图。我把它写出来并认为它会是这样的。
1-5(11)***********
6-10(8)********
11-15(12)************
16 -20(9)*********
21-25(10)**********
26-30( 11)***********
31-35(7)*******
36-40(8)** ******
41-45(13)*************
46-50(11)*** ********
我正在努力学习如何做到这一点而且我被卡住了。任何建议都会有帮助。
帮助。甚至一些可能的教程也会有所帮助。
I am trying to write a program that will generate 100 random numbers
between 1 and 50. Using these numbers, I want to generate a list that will
tell the number of random numbers that fell between 1-5, 6-10, 11-15,
16-20, ... , and 46-50. Finally,I would like to print out the results as a
histogram. I have written it out and would think it would look like this.
1-5 (11) ***********
6-10 (8) ********
11-15 (12) ************
16-20 (9) *********
21-25 (10) **********
26-30 (11) ***********
31-35 (7) *******
36-40 (8) ********
41-45 (13) *************
46-50 (11) ***********
I am trying to learn how to do this and I am stuck. Any suggestions would
help. Even some possible tutorials to chek out would help.
要从set [1,50]生成随机数整数,您可以使用:" number
= rand()%50 + 1;" (记得初始化伪随机数
生成器,例如srand(time(0)))。
要获得桶号,你可以使用 bucket =(number - 1)/ 5;" (所以在
中,最好使用
" number = rand()%50;"从set [0,49]生成随机整数然后计算桶号使用bucket =
number / 5;。
然后你增加给定的桶,即:桶[桶] ++" ;.当然,铲斗首先要归零。当结果准备就绪时,你使用两个嵌套的for循环打印
结果。
-
祝你好运,_ _
.o。 | Serenly Enlightened Majesty of o'',=。/`o
..o |计算机科学,Michalmina86 Nazarewicz(oo)
ooo + - < mina86 * tlen.pl> ---< jid:mina86 * chrome.pl> - ooO - (_) - Ooo--
To generate random number integer from set [1, 50] you can use: "number
= rand() % 50 + 1;" (remember to initialise pseudo-random number
generator with for instance srand(time(0))).
To get bucket number you can use "bucket = (number - 1) / 5;" (so in
fact it is better to generate random integer from set [0, 49] using
"number = rand() % 50;" and then calculate bucket number using "bucket =
number / 5;").
Then you increment given bucket, ie.: "buckets[bucket]++". Buckets have
to be zeroed first of course. And when results are ready you print the
results using two nested for loops.
--
Best regards, _ _
.o. | Liege of Serenly Enlightened Majesty of o'' \,=./ `o
..o | Computer Science, Michal "mina86" Nazarewicz (o o)
ooo +--<mina86*tlen.pl>---<jid:mina86*chrome.pl>--ooO--(_)--Ooo--
Michal Nazarewicz说:
Michal Nazarewicz said:
" joebenjamin" < gu ********* @ hotmail.comwrites:
"joebenjamin" <gu*********@hotmail.comwrites:
>我正在尝试编写一个可生成100个随机数的程序
将告诉1-5,16-10之间的随机数,11-15,16- 20,...,和46-50。最后,我想打印出
结果作为直方图。我已经把它写出来并认为它会是这样的。
1-5(11)***********
6-10 (8)********
11-15(12)************
16-20(9)******* **
21-25(10)**********
26-30(11)***********
31-35 (7)*******
36-40(8)********
41-45(13)************ *
46-50(11)***********
我正在努力学习如何做到这一点而且我被困住了。任何建议都会有所帮助。甚至一些可能的教程也会有所帮助。
>I am trying to write a program that will generate 100 random numbers
between 1 and 50. Using these numbers, I want to generate a list that
will tell the number of random numbers that fell between 1-5, 6-10,
11-15, 16-20, ... , and 46-50. Finally,I would like to print out the
results as a histogram. I have written it out and would think it
would look like this.
1-5 (11) ***********
6-10 (8) ********
11-15 (12) ************
16-20 (9) *********
21-25 (10) **********
26-30 (11) ***********
31-35 (7) *******
36-40 (8) ********
41-45 (13) *************
46-50 (11) ***********
I am trying to learn how to do this and I am stuck. Any suggestions
would help. Even some possible tutorials to chek out would help.
要从set [1,50]生成随机数整数,您可以使用:
" number = rand()%50 + 1;"
To generate random number integer from set [1, 50] you can use:
"number = rand() % 50 + 1;"
更好:int number = 50 *(rand()/(RAND_MAX + 1.0));
Better: int number = 50 * (rand() / (RAND_MAX + 1.0));
(记得使用例如srand(time(0))初始化伪随机
数字生成器)。
(remember to initialise pseudo-random
number generator with for instance srand(time(0))).
如果他想要重复性,他最好使用常数。如果他b $ b想要不可预测性(尽可能实用),以下函数
(基于Lawrence Kirby编写的代码)可以更好地完成
利用所有可用的熵。
#include< stddef.h>
#include< time.h>
#include< limits.h>
/ *用法:srand(time_seed()); * /
/ *根据当前时间选择并返回初始随机种子。
基于Lawrence Kirby的代码< fr ** @ genesis .demon.co.uk取代。 * /
未签名
time_seed(无效)
{
time_t timeval;
unsigned char * ptr;
未签名的种子;
size_t i;
timeval = time(NULL);
ptr =(unsigned char *)& timeval;
seed = 0;
for(i = 0; i< sizeof timeval; i ++)
seed = seed *(UCHAR_MAX + 2U)+ ptr [i];
返回种子;
}
If he wants repeatability, he''d be better off using a constant. If he
wants unpredictability (as far as is practical), the following function
(based on code written by Lawrence Kirby) does a better job of
utilising all the available entropy.
#include <stddef.h>
#include <time.h>
#include <limits.h>
/* Usage: srand (time_seed ()); */
/* Choose and return an initial random seed based on the current time.
Based on code by Lawrence Kirby <fr**@genesis.demon.co.uk>. */
unsigned
time_seed (void)
{
time_t timeval;
unsigned char *ptr;
unsigned seed;
size_t i;
timeval = time (NULL);
ptr = (unsigned char *) &timeval;
seed = 0;
for (i = 0; i < sizeof timeval; i++)
seed = seed * (UCHAR_MAX + 2U) + ptr[i];
return seed;
}
>
要获得桶号,您可以使用bucket =(number - 1)/ 5;; (所以在
中,最好使用
" number = rand()%50;"从set [0,49]生成随机整数然后计算桶号使用桶
=数字/ 5;。
然后你增加给定桶,即:桶[桶] ++" ;.铲斗
首先将
归零。当结果准备就绪时,使用两个嵌套的for循环打印结果
。
>
To get bucket number you can use "bucket = (number - 1) / 5;" (so in
fact it is better to generate random integer from set [0, 49] using
"number = rand() % 50;" and then calculate bucket number using "bucket
= number / 5;").
Then you increment given bucket, ie.: "buckets[bucket]++". Buckets
have
to be zeroed first of course. And when results are ready you print
the results using two nested for loops.
-
Richard Heathfield< http://www.cpax.org.uk>
电子邮件:-www。 + rjh @
谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>
Usenet是一个奇怪的放置" - dmr 1999年7月29日
--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
试试这个:
#include< stdio.h>
#include< stdlib.h>
#include< time.h>
int main(无效)
{
int matrix [10];
int i;
int x;
srand(time(NULL));
for(i = 0; i< 10; i ++)
matrix [i] = 0;
for(i = 0; i< 100; i ++){
x = rand()%50 + 1;
if(x< ; 6)
matrix [0] ++;
else if(x< 11)
matrix [1] ++;
else if(x< 16)
matrix [2] ++;
else if(x< 21)
matrix [3] ++;
else if(x< 26)
matrix [4] ++;
else if(x< ; 31)
matrix [5] ++;
else if(x <36)
matrix [6] ++;
else if(x< 41)
matrix [7] ++;
else if(x< 46)
matrix [8] ++;
else
matrix [9] ++;
}
printf (QUOT; 1-5 \t\t(%3d)",matrix [0]);
for(i = 0; i< matrix [0]; i ++)
printf(" *");
printf(" \ n");
printf(" 6-10 \\ \\ t \\ t(%3d)",matrix [1]);
for(i = 0; i< matrix [1]; i ++)
printf (" *");
printf(" \ n");
返回0;
}
Try this:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main(void)
{
int matrix[10];
int i;
int x;
srand(time(NULL));
for(i=0;i<10;i++)
matrix[i]=0;
for(i=0;i<100;i++){
x=rand()%50 + 1;
if(x<6)
matrix[0]++;
else if(x<11)
matrix[1]++;
else if(x<16)
matrix[2]++;
else if(x<21)
matrix[3]++;
else if(x<26)
matrix[4]++;
else if(x<31)
matrix[5]++;
else if(x<36)
matrix[6]++;
else if(x<41)
matrix[7]++;
else if(x<46)
matrix[8]++;
else
matrix[9]++;
}
printf("1-5 \t\t ( %3d ) ",matrix[0]);
for(i=0;i<matrix[0];i++)
printf("*");
printf("\n");
printf("6-10 \t\t ( %3d ) ",matrix[1]);
for(i=0;i<matrix[1];i++)
printf("*");
printf("\n");
return 0;
}
这篇关于任何人对此都有任何想法.....的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!