อยากได้โครงสร้างและตัวอย่างเกี่ยวกับการสุ่มตัวเลขของภาษาซีคะ
ใครรู้หรือว่ามีตัวอย่างรบกวนช่วยตอบด้วยนะคะขอบคุณค่ะ
อยากได้โครงสร้างและตัวอย่างเกี่ยวกับการสุ่มตัวเลขของภาษาซีคะ
Moderator: mindphp, ผู้ดูแลกระดาน
- umihoshi
- PHP Newbie
- โพสต์: 1
- ลงทะเบียนเมื่อ: 01/01/1970 7:00 am
- mindphp
- ผู้ดูแลระบบ MindPHP
- โพสต์: 41247
- ลงทะเบียนเมื่อ: 22/09/2008 6:18 pm
- ติดต่อ:
ลอง ดู ตัว อย่างนี้ครับ
Generating Random Numbers
The key function in generating random numbers is;
int random (int n);
which generates a random number in the range of 0 to n-1. For example;
y = random(100);
y will be in the range of 0 though 99.
Note that if you run the following program, again and again, the same three random numbers will be generated. That is, TurboC always seeds the random number generator with the same starting number.
This feature is great for debugging. However, it would terrible if you were designing software for the gaming industry as everyone would know what numbers were going to come up.
Therefore, the function "randomize()" may be used to seed the random number generator with a number which is developed from the system clock, which of course, is always changing.
/*
** Program RANDOM1.C
**
** Generates three random numbers in range of 0 to 99 and
** reports as to which is the largest and which is the smallest.
**
** Intended to show the use of functions randomize(), random() anf the
** use of functions.
**
** Peter H. Anderson, MSU, Feb 6, '97
*/
#include <stdio.h>
#include <stdlib.h> /* required for randomize() and random() */
#include <conio.h> /* required for clrscr() */
int gen_rand(void); /* note these are declarations of functions */
int find_max(int x, int y, int z);
int find_min(int x, int y, int z);
FILE *f1;
void main(void)
{
int num1, num2, num3, max, min;
clrscr(); /* clear the screen */
f1=fopen("a:\\output.dta", "wt"); /* open a file for output */
/* randomize(); */ /* uncomment this if you want a random start */
num1=gen_rand();
num2=gen_rand();
num3=gen_rand();
max=find_max(num1, num2, num3);
min=find_min(num1, num2, num3);
printf("Random numbers are %d, %d, and %d\n", num1, num2, num3);
fprintf(f1, "Random numbers are %d, %d, and %d\n", num1, num2, num3);
printf("Largest is %d. Smallest is %d.\n", max, min);
fprintf(f1, "Largest is %d. Smallest is %d.\n", max, min);
fclose(f1);
}
int gen_rand(void)
/* returns random number in range of 0 to 99 */
{
int n;
n=random(100); /* n is random number in range of 0 - 99 */
return(n);
}
int find_max( int x, int y, int z)
/* returns largest number */
{
int max;
if ((x>=y) && (x>=z))
{
max = x;
}
else if ((y>=x) && (y>=z))
{
max = y;
}
else
{
max = z;
}
return(max);
}
int find_min( int x, int y, int z)
/* returns smallest number */
{
int min;
if ((x<=y) && (x<=z))
{
min = x;
}
else if ((y<=x) && (y<=z))
{
min = y;
}
else
{
min = y;
}
return(min);
}
ที่มา http://www.phanderson.com/C/random.html
Generating Random Numbers
The key function in generating random numbers is;
int random (int n);
which generates a random number in the range of 0 to n-1. For example;
y = random(100);
y will be in the range of 0 though 99.
Note that if you run the following program, again and again, the same three random numbers will be generated. That is, TurboC always seeds the random number generator with the same starting number.
This feature is great for debugging. However, it would terrible if you were designing software for the gaming industry as everyone would know what numbers were going to come up.
Therefore, the function "randomize()" may be used to seed the random number generator with a number which is developed from the system clock, which of course, is always changing.
/*
** Program RANDOM1.C
**
** Generates three random numbers in range of 0 to 99 and
** reports as to which is the largest and which is the smallest.
**
** Intended to show the use of functions randomize(), random() anf the
** use of functions.
**
** Peter H. Anderson, MSU, Feb 6, '97
*/
#include <stdio.h>
#include <stdlib.h> /* required for randomize() and random() */
#include <conio.h> /* required for clrscr() */
int gen_rand(void); /* note these are declarations of functions */
int find_max(int x, int y, int z);
int find_min(int x, int y, int z);
FILE *f1;
void main(void)
{
int num1, num2, num3, max, min;
clrscr(); /* clear the screen */
f1=fopen("a:\\output.dta", "wt"); /* open a file for output */
/* randomize(); */ /* uncomment this if you want a random start */
num1=gen_rand();
num2=gen_rand();
num3=gen_rand();
max=find_max(num1, num2, num3);
min=find_min(num1, num2, num3);
printf("Random numbers are %d, %d, and %d\n", num1, num2, num3);
fprintf(f1, "Random numbers are %d, %d, and %d\n", num1, num2, num3);
printf("Largest is %d. Smallest is %d.\n", max, min);
fprintf(f1, "Largest is %d. Smallest is %d.\n", max, min);
fclose(f1);
}
int gen_rand(void)
/* returns random number in range of 0 to 99 */
{
int n;
n=random(100); /* n is random number in range of 0 - 99 */
return(n);
}
int find_max( int x, int y, int z)
/* returns largest number */
{
int max;
if ((x>=y) && (x>=z))
{
max = x;
}
else if ((y>=x) && (y>=z))
{
max = y;
}
else
{
max = z;
}
return(max);
}
int find_min( int x, int y, int z)
/* returns smallest number */
{
int min;
if ((x<=y) && (x<=z))
{
min = x;
}
else if ((y<=x) && (y<=z))
{
min = y;
}
else
{
min = y;
}
return(min);
}
ที่มา http://www.phanderson.com/C/random.html
ติดตาม VDO: http://www.youtube.com/c/MindphpVideoman
ติดตาม FB: https://www.facebook.com/pages/MindphpC ... 9517401606
หมวดแชร์ความรู้: https://www.mindphp.com/forums/viewforum.php?f=29
รับอบรม และพัฒนาระบบ: https://www.mindphp.com/forums/viewtopic.php?f=6&t=2042
ติดตาม FB: https://www.facebook.com/pages/MindphpC ... 9517401606
หมวดแชร์ความรู้: https://www.mindphp.com/forums/viewforum.php?f=29
รับอบรม และพัฒนาระบบ: https://www.mindphp.com/forums/viewtopic.php?f=6&t=2042
-
- Similar Topics
- ตอบกลับ
- แสดง
- โพสต์ล่าสุด
-
-
โพสต์ใหม่ Q - อยากได้ css tag มาเพิ่มความสวยงามต้องทำอย่างไงครับ
โดย Ittichai_chupol » 05/10/2018 6:12 pm » ใน Programming - PHP - 1 ตอบกลับ
- 1504 แสดง
-
โพสต์ล่าสุด โดย Parichat
05/10/2018 6:38 pm
-
-
- 3 ตอบกลับ
- 1901 แสดง
-
โพสต์ล่าสุด โดย น้องชะอมชุบไข่
05/02/2015 12:50 am
-
-
โพสต์ใหม่ อยากได้ G-shock ตัวยักษ์ดำ อยากทราบว่าดูยังไงว่าแท้ไม่แท้
โดย Kao Kub » 29/07/2019 4:07 pm » ใน พูดคุยเรื่องทั่วไป จับฉ่าย - 0 ตอบกลับ
- 535 แสดง
-
โพสต์ล่าสุด โดย Kao Kub
29/07/2019 4:07 pm
-
-
-
โพสต์ใหม่ สอบถาม SQl อยากได้ id ล่าสุดของ Id ที่เรา where in
โดย thatsawan » 08/05/2017 1:34 pm » ใน SQL - Database - 1 ตอบกลับ
- 1718 แสดง
-
โพสต์ล่าสุด โดย tsukasaz
08/05/2017 1:53 pm
-
-
-
โพสต์ใหม่ อยากได้ โน้ตบุ๊คเล่นเกม ราคาไม่แพง เลือกยังไงดี !
โดย asider8629 » 18/09/2023 6:54 pm » ใน Mobile Programming - Android, iOS, Window Phone - 0 ตอบกลับ
- 1418 แสดง
-
โพสต์ล่าสุด โดย asider8629
18/09/2023 6:54 pm
-
-
-
โพสต์ใหม่ สอบถามครับ อยากได้ url ของลิงค์ ให้มาแสดงในช่อง ต้องทำยังไงครับ
โดย chatee supasand » 09/10/2019 12:22 pm » ใน Programming - C/C++ & java & Python - 6 ตอบกลับ
- 1399 แสดง
-
โพสต์ล่าสุด โดย chatee supasand
09/10/2019 1:41 pm
-
-
-
โพสต์ใหม่ อยากได้ jquery gallery ajax popup ใครมีบ้าง
โดย thatsawan » 01/03/2016 10:57 am » ใน JavaScript & jQuery Ajax & Node.JS - 2 ตอบกลับ
- 2408 แสดง
-
โพสต์ล่าสุด โดย thatsawan
02/03/2016 4:44 pm
-
ผู้ใช้งานขณะนี้
สมาชิกกำลังดูบอร์ดนี้: ไม่มีสมาชิกใหม่ และบุคลทั่วไป 107