Linux下C语言复现扔色子程序(转)
Posted zhangrelay
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux下C语言复现扔色子程序(转)相关的知识,希望对你有一定的参考价值。
参考:www.zhihu.com/question/386347847/answer/2576561977
效果如下:
程序如下:
#include <stdlib.h>
#include <stdio.h>
#include <memory.h>
#include <math.h>
#include <unistd.h>
#define pi 3.14159265358979323846
#define c 3
#define screen_width 50
#define screen_height 25
float cube[6][4][3] =
-0.5, -0.5, 0.5, 0.5, -0.5, 0.5, -0.5, 0.5, 0.5, 0.0, 0.0, 1.0
,
-0.5, -0.5, 0.5, -0.5, -0.5, -0.5, -0.5, 0.5, 0.5, -1.0, 0.0, 0.0
,
-0.5, -0.5, 0.5, -0.5, -0.5, -0.5, 0.5, -0.5, 0.5, 0.0, -1.0, 0.0
,
-0.5, 0.5, 0.5, 0.5, 0.5, 0.5, -0.5, 0.5, -0.5, 0.0, 1.0, 0.0
,
0.5, -0.5, 0.5, 0.5, -0.5, -0.5, 0.5, 0.5, 0.5, 1.0, 0.0, 0.0
,
-0.5, -0.5, -0.5, 0.5, -0.5, -0.5, -0.5, 0.5, -0.5, 0.0, 0.0, -1.0
;
int face[6][3][3] =
0, 0, 0, 0, 1, 0, 0, 0, 0
,
0, 0, 1, 0, 0, 0, 1, 0, 0
,
0, 0, 1, 0, 1, 0, 1, 0, 0
,
1, 0, 1, 0, 0, 0, 1, 0, 1
,
1, 0, 1, 0, 1, 0, 1, 0, 1
,
1, 0, 1, 1, 0, 1, 1, 0, 1
;
int judgeFace(int ID, float x, float y)
return face[ID][(int)(3.f * y)][(int)(3.f * x)];
void ini()
for (int i = 0; i < 6; i++)
for (int j = 0; j < 4; j++)
float x = cube[i][j][0];
float y = cube[i][j][1];
float z = cube[i][j][2];
cube[i][j][0] = (sqrt(3) / 6.f + 0.5) * x - sqrt(3) / 3.f * y + (-0.5 + sqrt(3) / 6.f) * z;
cube[i][j][1] = (sqrt(3) / 3.f) * x + (sqrt(3) / 3.f) * y + (sqrt(3) / 3.f) * z;
cube[i][j][2] = (-0.5 + sqrt(3) / 6.f) * x + (-sqrt(3) / 3.f) * y + (sqrt(3) / 6.f + 0.5) * z;
void renderFrame()
ini();
double time = 0;
while (1)
time = time + 0.01;
float z_buffer[screen_height + 1][screen_width + 1];
for (int i = 0; i <= screen_height; i++)
for (int j = 0; j <= screen_width; j++)
z_buffer[i][j] = -100;
char output[screen_height + 1][screen_width + 1];
memset(output, ' ', sizeof(output));
for (int i = 0; i < 6; i++)
for (float u = 0.f; u < 1.f; u = u + 0.01)
for (float v = 0.f; v < 1.f; v = v + 0.01)
float m_x = (cube[i][1][0] - cube[i][0][0]);
float m_y = (cube[i][1][1] - cube[i][0][1]);
float m_z = (cube[i][1][2] - cube[i][0][2]);
float n_x = (cube[i][2][0] - cube[i][0][0]);
float n_y = (cube[i][2][1] - cube[i][0][1]);
float n_z = (cube[i][2][2] - cube[i][0][2]);
float x = m_x * u + n_x * v + cube[i][0][0];
float y = m_y * u + n_y * v + cube[i][0][1];
float z = m_z * u + n_z * v + cube[i][0][2];
float rotation_x = cos(time) * x - sin(time) * z;
float rotation_y = y;
float rotation_z = sin(time) * x + cos(time) * z;
float normal_x = (cube[i][3][0]) * cos(time) - sin(time) * (cube[i][3][2]);
float normal_y = cube[i][3][1];
float normal_z = (cube[i][3][0]) * sin(time) + cos(time) * (cube[i][3][2]);
int screen_x = (rotation_x / (1 - rotation_z / c) + 1.f) / 2 * screen_width;
int screen_y = (rotation_y / (1 - rotation_z / c) + 1.f) / 2 * screen_height;
float screen_z = rotation_z / (1 - rotation_z / c);
float L = normal_z;
if (L > 0)
if (z_buffer[screen_y][screen_x] < screen_z)
z_buffer[screen_y][screen_x] = screen_z;
if (judgeFace(i, u, v) == 1)
float tempu = u - (float)((int)(u * 3.f)) * 1.f / 3.f;
float tempv = v - (float)((int)(v * 3.f)) * 1.f / 3.f;
if ((tempu - 1.f / 6.f) * (tempu - 1.f / 6.f) + (tempv - 1.f / 6.f) * (tempv - 1.f / 6.f) <= 1.f / 36.f)
L = 0;
else
L = (L + 0.1) * sqrt(2);
else
L = (L + 0.1) * sqrt(2);
int luminance_index = L * 8;
if (luminance_index > 11)
luminance_index = 11;
output[screen_y][screen_x] = ".,-~:;=!*#$@"[luminance_index];
else
if (z_buffer[screen_y][screen_x] < screen_z)
z_buffer[screen_y][screen_x] = screen_z;
for (int j = screen_height; j >= 0; j--)
for (int i = 0; i <= screen_width; i++)
putchar(output[j][i]);
putchar('\\n');
usleep(15000);
printf("\\x1b[26A");
int main()
renderFrame();
return 0;
$ g++ dice.cpp -o dice
$ ./dice
另一种如下:
此应用程序使用 srand() 函数来播种随机数生成器。函数 Random(n) 返回一个 1 到 n 范围内的整数。
int 数组 totals 保存分数 3 到 18 的总计数。然后循环 1000 万次。此数字被定义为 const,但如果您的编译器不支持 const,请取消注释 #define。
每个骰子 d1、d2 和 d3 保存 Random() 生成的骰子掷骰子,并且组合骰子得分的元素(在 3-18 范围内)递增。
最后一部分打印出总数,以查看它是否根据概率生成投掷。一个 6 面骰子的平均得分为 3.5,因此三个骰子的平均得分应该在 10.5 左右。 10 和 11 的总数大致相同,发生率约为 12.5%。
这是典型运行的输出。它不会超过一秒钟。
掷一千万个骰子
3 46130
4 138608
5 277278
6 462607
7 695381
8 972020
9 1158347
10 1253671
11 1249267
12 1156480
13 972005
14 692874
15 462452
16 277575
17 139142
18 46163
// dicerolls.c :
#include <time.h> /* Needed just for srand seed */
#include <stdlib.h>
#include <stdio.h>
const tenmillion = 1000000L;
/* #define tenmillion 10000000L */
void Randomize()
srand( (unsigned)time( NULL ) ) ;
int Random(int Max)
return ( rand() % Max)+ 1;
int main(int argc, char* argv[])
int i;
int totals[19];
printf("Rolling Ten Million Dice\\n") ;
Randomize() ;
for (i=3;i<=18;i++)
totals[ i ]=0;
for (i=0;i< tenmillion;i++)
int d1=Random(6) ;
int d2=Random(6) ;
int d3=Random(6) ;
int total=d1+d2+d3;
totals[ total ]++;
for (i=3;i<=18;i++)
printf("%i %i\\n\\r",i,totals[ i ]) ;
return 0;
以上是关于Linux下C语言复现扔色子程序(转)的主要内容,如果未能解决你的问题,请参考以下文章
把一个骰子扔n次, n次朝上一面的点数和为s。 输入n, 打印出s的所有可能的值出现的概率。
2022-10-30:给你一个长度为 n 的整数数组 rolls 和一个整数 k 。 你扔一个 k 面的骰子 n 次,骰子的每个面分别是 1 到 k , 其中第 i 次扔得到的数字是 rolls[i]