Linux下rand()/RAND_MAX

本文深入探讨了Linux环境下rand()函数在不同形式除法操作中产生的数值特性,对比分析了Windows与Linux的RAND_MAX差异,并揭示了其背后的数学原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在Linux平台下:

rand()/RAND_MAX 为0,(float)(rand()/RAND_MAX) 为0,(float)rand()/RAND_MAX 为小数。

我认为在Linux下分子为整数的话,除得的结果自然是整数了。

 

Linux下RAND_MAX和Windows下的RAND_MAX(都是32位操作系统):

window下的RAND_MAX为:0x7fff=2^15-1

linxu下的RAND_MAX为:2^31-1   加上一个正数最高位就为1=负数,即 - RAND_MAX=RAND_MAX+1所以 - rand()/RAND_MAX=rand()/(RAND_MAX+1)

 

# 检查当前环境状态 import sys, platform print("="*50) print("环境诊断报告") print("="*50) print(f"操作系统: {platform.platform()}") print(f"Python 版本: {sys.version}") print(f"Python 路径: {sys.executable}") # 检查包版本 try: import numpy, scipy, sklearn, joblib print(f"\nnumpy 版本: {numpy.__version__}") print(f"scipy 版本: {scipy.__version__}") print(f"scikit-learn 版本: {sklearn.__version__}") print(f"joblib 版本: {joblib.__version__}") except ImportError as e: print(f"\n包导入错误: {str(e)}") # 检查编译器 !which gcc !gcc --version ================================================= 环境诊断报告 ================================================== 操作系统: Linux-3.10.0-1160.108.1.el7.x86_64-x86_64-with-glibc2.10 Python 版本: 3.8.19 | packaged by conda-forge | (default, Mar 20 2024, 12:47:35) [GCC 12.3.0] Python 路径: /opt/conda/envs/mmedu/bin/python 包导入错误: libgomp-a34b3233.so.1.0.0: cannot open shared object file: No such file or directory ___________________________________________________________________________ Contents of /opt/conda/envs/mmedu/lib/python3.8/site-packages/sklearn/__check_build: __init__.py __pycache__ _check_build.cpython-38-x86_64-linux-gnu.so ___________________________________________________________________________ It seems that scikit-learn has not been built correctly. If you have installed scikit-learn from source, please do not forget to build the package before using it: run `python setup.py install` or `make` in the source directory. If you have used an installer, please check that it is suited for your Python version, your operating system and your platform. /usr/bin/gcc gcc (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0 Copyright (C) 2023 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. Error compiling Cython file: ------------------------------------------------------------ ... # Max value for our rand_r replacement (near the bottom). # We don't use RAND_MAX because it's different across platforms and # particularly tiny on Windows/MSVC. RAND_R_MAX = 0x7FFFFFFF cpdef sample_without_replacement(np.int_t n_population, ^ ------------------------------------------------------------ sklearn/utils/_random.pxd:18:33: 'int_t' is not a type identifier Error compiling Cython file: ------------------------------------------------------------ ... # We don't use RAND_MAX because it's different across platforms and # particularly tiny on Windows/MSVC. RAND_R_MAX = 0x7FFFFFFF cpdef sample_without_replacement(np.int_t n_population, np.int_t n_samples, ^ ------------------------------------------------------------ sklearn/utils/_random.pxd:19:33: 'int_t' is not a type identifier Traceback (most recent call last): File "/tmp/pip-build-env-sg2x2g64/overlay/lib/python3.10/site-packages/Cython/Build/Dependencies.py", line 1306, in cythonize_one_helper return cythonize_one(*m) File "/tmp/pip-build-env-sg2x2g64/overlay/lib/python3.10/site-packages/Cython/Build/Dependencies.py", line 1298, in cythonize_one raise CompileError(None, pyx_file) Cython.Compiler.Errors.CompileError: sklearn/utils/_seq_dataset.pyx [49/53] Cythonizing sklearn/utils/_weight_vector.pyx performance hint: sklearn/utils/_weight_vector.pyx:169:29: Exception check after calling 'reset_wscale' will always require the GIL to be acquired. Possible solutions: 1. Declare 'reset_wscale' as 'noexcept' if you control the definition and you're sure you don't want the function to raise exceptions. 2. Use an 'int' return type on 'reset_wscale' to allow an error code to be returned. performance hint: sklearn/utils/_weight_vector.pyx:174:17: Exception check after calling '__pyx_fuse_1_axpy' will always require the GIL to be acquired. Possible solutions: 1. Declare '__pyx_fuse_1_axpy' as 'noexcept' if you control the definition and you're sure you don't want the function to raise exceptions. 2. Use an 'int' return type on '__pyx_fuse_1_axpy' to allow an error code to be returned. performance hint: sklearn/utils/_weight_vector.pyx:176:17: Exception check after calling '__pyx_fuse_1_scal' will always require the GIL to be acquired. Possible solutions: 1. Declare '__pyx_fuse_1_scal' as 'noexcept' if you control the definition and you're sure you don't want the function to raise exceptions. 2. Use an 'int' return type on '__pyx_fuse_1_scal' to allow an error code to be returned. performance hint: sklearn/utils/_weight_vector.pyx:180:13: Exception check after calling '__pyx_fuse_1_scal' will always require the GIL to be acquired. Possible solutions: 1. Declare '__pyx_fuse_1_scal' as 'noexcept' if you control the definition and you're sure you don't want the function to raise exceptions. 2. Use an 'int' return type on '__pyx_fuse_1_scal' to allow an error code to be returned. [50/53] Cythonizing sklearn/utils/arrayfuncs.pyx [51/53] Cythonizing sklearn/utils/graph_shortest_path.pyx [52/53] Cythonizing sklearn/utils/murmurhash.pyx [53/53] Cythonizing sklearn/utils/sparsefuncs_fast.pyx multiprocessing.pool.RemoteTraceback: """ Traceback (most recent call last): File "/opt/conda/lib/python3.10/multiprocessing/pool.py", line 125, in worker result = (True, func(*args, **kwds)) File "/opt/conda/lib/python3.10/multiprocessing/pool.py", line 48, in mapstar return list(map(*args)) File "/tmp/pip-build-env-sg2x2g64/overlay/lib/python3.10/site-packages/Cython/Build/Dependencies.py", line 1306, in cythonize_one_helper return cythonize_one(*m) File "/tmp/pip-build-env-sg2x2g64/overlay/lib/python3.10/site-packages/Cython/Build/Dependencies.py", line 1298, in cythonize_one raise CompileError(None, pyx_file) Cython.Compiler.Errors.CompileError: sklearn/ensemble/_hist_gradient_boosting/splitting.pyx """ The above exception was the direct cause of the following exception: Traceback (most recent call last): File "/opt/conda/lib/python3.10/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 389, in <module> main() File "/opt/conda/lib/python3.10/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 373, in main json_out["return_val"] = hook(**hook_input["kwargs"]) File "/opt/conda/lib/python3.10/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 175, in prepare_metadata_for_build_wheel return hook(metadata_directory, config_settings) File "/tmp/pip-build-env-sg2x2g64/overlay/lib/python3.10/site-packages/setuptools/build_meta.py", line 374, in prepare_metadata_for_build_wheel self.run_setup() File "/tmp/pip-build-env-sg2x2g64/overlay/lib/python3.10/site-packages/setuptools/build_meta.py", line 512, in run_setup super().run_setup(setup_script=setup_script) File "/tmp/pip-build-env-sg2x2g64/overlay/lib/python3.10/site-packages/setuptools/build_meta.py", line 317, in run_setup exec(code, locals()) File "<string>", line 301, in <module> File "<string>", line 297, in setup_package File "/tmp/pip-build-env-sg2x2g64/overlay/lib/python3.10/site-packages/numpy/distutils/core.py", line 135, in setup config = configuration() File "<string>", line 188, in configuration File "/tmp/pip-build-env-sg2x2g64/overlay/lib/python3.10/site-packages/numpy/distutils/misc_util.py", line 1041, in add_subpackage config_list = self.get_subpackage(subpackage_name, subpackage_path, File "/tmp/pip-build-env-sg2x2g64/overlay/lib/python3.10/site-packages/numpy/distutils/misc_util.py", line 1007, in get_subpackage config = self._get_configuration_from_setup_py( File "/tmp/pip-build-env-sg2x2g64/overlay/lib/python3.10/site-packages/numpy/distutils/misc_util.py", line 949, in _get_configuration_from_setup_py config = setup_module.configuration(*args) File "/tmp/pip-install-rrvbwdb9/scikit-learn_80fc3ca03e3e46e49d2190da23c8cd17/sklearn/setup.py", line 83, in configuration cythonize_extensions(top_path, config) File "/tmp/pip-install-rrvbwdb9/scikit-learn_80fc3ca03e3e46e49d2190da23c8cd17/sklearn/_build_utils/__init__.py", line 70, in cythonize_extensions config.ext_modules = cythonize( File "/tmp/pip-build-env-sg2x2g64/overlay/lib/python3.10/site-packages/Cython/Build/Dependencies.py", line 1145, in cythonize result.get(99999) # seconds File "/opt/conda/lib/python3.10/multiprocessing/pool.py", line 774, in get raise self._value Cython.Compiler.Errors.CompileError: sklearn/ensemble/_hist_gradient_boosting/splitting.pyx [end of output] note: This error originates from a subprocess, and is likely not a problem with pip. error: metadata-generation-failed × Encountered error while generating package metadata. ╰─> See above for output. note: This is an issue with the package mentioned above, not pip. hint: See above for details.
08-01
#include <stdio.h> #include <conio.h> #include <stdlib.h> #include <time.h> #include <string.h> #define MM 8 /* RS code over GF(2**8) */ #define KK 239 /* Number of information symbols */ #define NN 255 /* Total codeword length */ #define TT 8 /* Error correction capability */ int alpha_to[NN + 1]; /* Exponent to element mapping */ int index_of[NN + 1]; /* Element to exponent mapping */ int gg[NN - KK + 1]; /* Generator polynomial */ int recd[NN]; /* Received codeword */ int err_flag; /* Error flag */ int data[KK], bb[NN - KK]; /* Data and parity symbols */ /* GF multiplication */ int gf_mult(int a, int b) { if (a == 0 || b == 0) return 0; return alpha_to[(index_of[a] + index_of[b]) % NN]; } /* GF inverse */ int gf_inv(int a) { if (a == 0) return 0; return alpha_to[(NN - index_of[a]) % NN]; } /* Generate GF(2^8) */ void generate_gf() { int pp[9] = { 1, 0, 1, 1, 1, 0, 0, 0, 1 }; /* Primitive polynomial */ register int i, mask = 1; alpha_to[MM] = 0; for (i = 0; i < MM; i++) { alpha_to[i] = mask; index_of[alpha_to[i]] = i; if (pp[i] != 0) alpha_to[MM] ^= mask; mask <<= 1; } index_of[alpha_to[MM]] = MM; mask >>= 1; for (i = MM + 1; i < NN; i++) { if (alpha_to[i - 1] >= mask) alpha_to[i] = alpha_to[MM] ^ ((alpha_to[i - 1] ^ mask) << 1); else alpha_to[i] = alpha_to[i - 1] << 1; index_of[alpha_to[i]] = i; } index_of[0] = -1; } /* Generate generator polynomial */ void gen_poly() { register int i, j; gg[0] = 2; /* α^1 */ gg[1] = 1; /* g(x) = x + α^1 */ for (i = 2; i <= NN - KK; i++) { gg[i] = 1; for (j = i - 1; j > 0; j--) { if (gg[j] != 0) gg[j] = gg[j - 1] ^ alpha_to[(index_of[gg[j]] + i) % NN]; else gg[j] = gg[j - 1]; } gg[0] = alpha_to[(index_of[gg[0]] + i) % NN]; } /* Convert to index form */ for (i = 0; i <= NN - KK; i++) gg[i] = index_of[gg[i]]; } /* RS encoding */ void encode_rs() { register int i, j; int feedback; for (i = 0; i < NN - KK; i++) bb[i] = 0; for (i = KK - 1; i >= 0; i--) { feedback = index_of[data[i] ^ bb[NN - KK - 1]]; if (feedback != -1) { for (j = NN - KK - 1; j > 0; j--) { if (gg[j] != -1) bb[j] = bb[j - 1] ^ alpha_to[(gg[j] + feedback) % NN]; else bb[j] = bb[j - 1]; } bb[0] = alpha_to[(gg[0] + feedback) % NN]; } else { for (j = NN - KK - 1; j > 0; j--) bb[j] = bb[j - 1]; bb[0] = 0; } } } /* Compute polynomial degree */ int poly_degree(int poly[], int len) { int deg = -1; for (int i = len - 1; i >= 0; i--) { if (poly[i] != 0) { deg = i; break; } } return deg; } /* Polynomial multiplication with mod x^limit */ void poly_mult(int a[], int a_deg, int b[], int b_deg, int result[], int limit) { for (int i = 0; i < limit; i++) result[i] = 0; for (int i = 0; i <= a_deg; i++) { if (a[i] != 0) { for (int j = 0; j <= b_deg && i + j < limit; j++) { if (b[j] != 0) { result[i + j] ^= gf_mult(a[i], b[j]); } } } } } /* Copy polynomial */ void copy_poly(int src[], int dest[], int len) { for (int i = 0; i < len; i++) { dest[i] = src[i]; } } /* RS decoding with Euclidean algorithm */ void decode_rs() { int s[NN - KK + 1]; /* Syndrome values */ int root[TT], loc[TT], err[NN]; int erasures[2 * TT]; /* Erasure positions */ int erasure_count = 0; /* Number of erasures */ int syn_error = 0; /* Syndrome error flag */ register int i, j; /* Erasure detection */ for (i = 0; i < NN; i++) { if (recd[i] == -1 && erasure_count < 2 * TT) { erasures[erasure_count++] = i; } } /* Syndrome calculation */ for (i = 1; i <= NN - KK; i++) { s[i] = 0; for (j = 0; j < NN; j++) { if (recd[j] == -1) continue; if (recd[j] != 0) { int exp = (index_of[recd[j]] + i * j) % NN; if (exp < 0) exp += NN; s[i] ^= alpha_to[exp]; } } if (s[i] != 0) syn_error = 1; s[i] = (s[i] == 0) ? -1 : index_of[s[i]]; } if (syn_error || erasure_count > 0) { err_flag = 1; /* Allocate memory for polynomials */ int gamma[NN - KK + 1] = { 0 }; /* Erasure locator polynomial */ int T[NN - KK] = { 0 }; /* Modified syndrome polynomial */ int max_deg = 2 * (NN - KK) + 1; /* 33 */ int R0[65] = { 0 }; int R1[65] = { 0 }; int V0[65] = { 0 }; int V1[65] = { 0 }; int lambda[NN - KK + 1] = { 0 };/* Error locator polynomial */ int omega[NN - KK] = { 0 }; /* Error evaluator polynomial */ /* Initialize gamma = 1 (erasure locator polynomial) */ gamma[0] = 1; if (erasure_count > 0) { for (i = 0; i < erasure_count; i++) { int Xi = alpha_to[erasures[i] % NN]; int tmp[NN - KK + 1] = { 0 }; tmp[0] = gamma[0]; for (j = 1; j <= NN - KK; j++) { tmp[j] = gamma[j] ^ gf_mult(Xi, gamma[j - 1]); } for (j = 0; j <= NN - KK; j++) { gamma[j] = tmp[j]; } } } /* Convert syndrome to polynomial form */ int S_poly[NN - KK]; for (i = 0; i < NN - KK; i++) { if (s[i + 1] == -1) { S_poly[i] = 0; } else { S_poly[i] = alpha_to[s[i + 1]]; } } /* Compute modified syndrome T(x) = S(x) * gamma(x) mod x^(2t) */ int gamma_deg = poly_degree(gamma, NN - KK + 1); poly_mult(S_poly, NN - KK - 1, gamma, gamma_deg, T, NN - KK); /* Initialize Euclidean algorithm */ R0[NN - KK] = 1; /* R0 = x^(2t) */ int deg_R0 = NN - KK; /* Degree of x^(2t) */ for (i = 0; i < NN - KK; i++) { R1[i] = T[i]; } int deg_R1 = poly_degree(R1, max_deg); V1[0] = 1; /* V1 = 1 */ int deg_V0 = -1; /* V0 = 0 */ int deg_V1 = 0; /* Degree of V1 is 0 */ int v_max = (NN - KK - erasure_count) / 2; /* Max errors correctable */ /* Euclidean algorithm iteration */ while (deg_R1 != -1 && deg_R1 >= v_max) { if (deg_R0 < deg_R1) { /* Swap R0 and R1 */ int temp_deg = deg_R0; deg_R0 = deg_R1; deg_R1 = temp_deg; int temp_R[65]; copy_poly(R0, temp_R, max_deg); copy_poly(R1, R0, max_deg); copy_poly(temp_R, R1, max_deg); /* Swap V0 and V1 */ temp_deg = deg_V0; deg_V0 = deg_V1; deg_V1 = temp_deg; int temp_V[65]; copy_poly(V0, temp_V, max_deg); copy_poly(V1, V0, max_deg); copy_poly(temp_V, V1, max_deg); } int shift = deg_R0 - deg_R1; int factor = gf_mult(R0[deg_R0], gf_inv(R1[deg_R1])); /* Update R0: R0 = R0 + factor * x^shift * R1 */ for (i = 0; i <= deg_R1; i++) { if (R1[i] != 0 && i + shift < max_deg) { R0[i + shift] ^= gf_mult(factor, R1[i]); } } deg_R0 = poly_degree(R0, max_deg); /* Update V0: V0 = V0 + factor * x^shift * V1 */ for (i = 0; i <= deg_V1; i++) { if (V1[i] != 0 && i + shift < max_deg) { if (i + shift < max_deg) { V0[i + shift] ^= gf_mult(factor, V1[i]); } } } deg_V0 = poly_degree(V0, max_deg); } /* Error locator polynomial = V1 (lambda) */ for (i = 0; i <= deg_V1; i++) { lambda[i] = V1[i]; } int deg_lambda = deg_V1; /* Error evaluator polynomial = R1 (omega) */ for (i = 0; i <= deg_R1; i++) { omega[i] = R1[i]; } int deg_omega = deg_R1; /* Compute total error locator (gamma * lambda) */ int total_loc[2 * TT + 1] = { 0 }; poly_mult(gamma, gamma_deg, lambda, deg_lambda, total_loc, 2 * TT + 1); int deg_total = poly_degree(total_loc, 2 * TT + 1); /* Find roots of total_loc (Chien search) */ int count = 0; for (int pos = 0; pos < NN; pos++) { int Xi_inv = alpha_to[(NN - pos) % NN]; int sum = total_loc[0]; /* Evaluate polynomial at alpha^(-pos) */ int Xi_power = Xi_inv; for (i = 1; i <= deg_total; i++) { if (total_loc[i] != 0) { sum ^= gf_mult(total_loc[i], Xi_power); } Xi_power = gf_mult(Xi_power, Xi_inv); } if (sum == 0) { loc[count] = pos; root[count] = Xi_inv; /* alpha^(-pos) */ count++; if (count > deg_total) break; /* Stop if found more roots than degree */ } } if (count != deg_total || deg_total == -1) { err_flag = 2; /* Too many errors or no roots found */ } else { /* Initialize error array */ for (i = 0; i < NN; i++) { err[i] = 0; if (recd[i] == -1) recd[i] = 0; } /* Forney algorithm for error values */ for (i = 0; i < count; i++) { int pos = loc[i]; int Xi = root[i]; /* alpha^(-pos) */ /* Compute numerator (omega(Xi)) */ int num = 0; int Xi_power = 1; for (j = 0; j <= deg_omega; j++) { if (omega[j] != 0) { num ^= gf_mult(omega[j], Xi_power); } Xi_power = gf_mult(Xi_power, Xi); } /* Compute denominator (lambda'(Xi)) */ int denom = 0; for (j = 1; j <= deg_total; j += 2) { /* Only odd powers */ if (total_loc[j] != 0) { /* Derivative term: j * loc[j] * Xi^(j-1) */ int deriv = gf_mult(total_loc[j], alpha_to[(j - 1) * index_of[Xi] % NN]); denom ^= deriv; } } if (denom != 0) { int err_val = gf_mult(num, gf_inv(denom)); recd[pos] ^= err_val; } else { err_flag = 2; break; } } } } else { err_flag = 0; /* No errors */ } } /* Print hex array */ void print_hex_array(int arr[], int start, int count) { for (int i = 0; i < count; i++) { printf("%02X ", arr[start + i]); } printf("\n"); } int main() { int acterr_num, err_pos[TT]; srand((unsigned)time(NULL)); generate_gf(); gen_poly(); do { printf("\nReed-Solomon (255,239) 解码测试\n"); printf("最大纠错能力: %d 个错误\n", TT); printf("输入注入错误数量(0-%d): ", TT); scanf_s("%d", &acterr_num); if (acterr_num > TT) { printf("错误数量超过纠错能力!\n"); continue; } /* 生成随机数据 */ for (int i = 0; i < KK; i++) data[i] = rand() % (NN + 1); /* 编码 */ encode_rs(); /* 构建码字 */ for (int i = 0; i < NN - KK; i++) recd[i] = bb[i]; for (int i = NN - KK; i < NN; i++) recd[i] = data[i - (NN - KK)]; /* 保存原始码字 */ int recd_copy[NN]; for (int i = 0; i < NN; i++) recd_copy[i] = recd[i]; /* 显示原始数据 */ printf("\n原始数据(前10个符号):\n"); print_hex_array(recd, NN - KK, 10); /* 手动输入错误位置 */ if (acterr_num > 0) { printf("输入错误位置(0-%d):\n", NN - 1); for (int i = 0; i < acterr_num; i++) { printf("错误 %d 位置: ", i + 1); scanf_s("%d", &err_pos[i]); if (err_pos[i] < 0 || err_pos[i] >= NN) { printf("位置无效!\n"); i--; continue; } } } /* 注入错误 */ printf("\n注入%d个错误...\n", acterr_num); for (int i = 0; i < acterr_num; i++) { int err_val = 1 + rand() % (NN - 1); // 非零错误值 int original = recd[err_pos[i]]; recd[err_pos[i]] ^= err_val; printf(" 位置 %d: %02X → %02X (错误值 = %02X)\n", err_pos[i], original, recd[err_pos[i]], err_val); } /* 解码 */ decode_rs(); /* 显示解码结果 */ printf("\n状态: "); switch (err_flag) { case 0: printf("未检测到错误"); break; case 1: printf("错误纠正成功"); break; case 2: printf("错误超出能力"); break; default: printf("未知状态"); } printf("\n\n解码后数据(前10个符号):\n"); print_hex_array(recd, NN - KK, 10); printf("原始数据(前10个符号):\n"); print_hex_array(recd_copy, NN - KK, 10); /* 验证结果 */ int errors = 0; for (int i = NN - KK; i < NN; i++) { if (recd[i] != recd_copy[i]) { printf("位置 %d: 原始值=%02X, 解码值=%02X\n", i, recd_copy[i], recd[i]); errors++; } } if (errors == 0) { printf("\n验证: 所有数据符号与原始一致!\n"); } else { printf("\n验证: %d个数据符号与原始不同\n", errors); } printf("\n按ESC退出,其他键继续..."); } while (_getch() != 27); return 0; }
最新发布
08-15
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值