验证大语言模型不会算数但可以编写算数的程序

发布于:2025-07-19 ⋅ 阅读:(18) ⋅ 点赞:(0)

摘要:

        本文通过几个实例测试了大语言模型在数学计算、排序、统计等方面的能力。结果显示,对于简单字符统计、排序等任务,大模型能正确生成实现代码,但当数据区分度降低时容易出错。在计算学生分数排名任务中,大模型生成的代码能正确找出最高分学生,但人工验证发现当分数相近时模型容易出错,表明其本质是基于概率的"快速判断"而非精确计算。文章还提供了用于生成测试数据的Java代码,展示了如何模拟学生成绩数据来测试模型性能。

例子1:统计字符出现次数

提示词:

请计算下面的字符串中数字0、1、2、3、4、5、6、7、8、9分别出现的次数:226907753108255933922278827494564064174739734930912465086691634631952723930270479140904052165942993294647212044377162873817647756142026806142597787926876264861380224440379927543802320580625784426007927955993320852739118400356448384366396329041614889063292067888366353036383063654754492436993137361121703178486219583117745209951459144854543144217112830979388614605805088577525947858099661844737381545915893113356524803295259661251464978316757787488376077844685558167255404535620027212670175512611302897522842178309043476982087953352305028940148319688670184979900065901326248807783611098051333540644550847570585295921219868494383960132354706526867722654073994632827811635833982528675681693122162264296479646868167451532626094165658151374368943293717618994082590595300167956594419075879793081978367883730780474672584089964561437266342743784511770459136509197136978309500601526453502259017792533495596964660043261271943895075142841483160040767563720144699907054266156661758811063241406677

 红框中大模型的输出明显算错了,其实也没有那么明显,自己可以写程序验证一下,但是它编写的程序可以算出正确答案:

大模型变写的 javascript 代码:


const str = "226907753108255933922278827494564064174739734930912465086691634631952723930270479140904052165942993294647212044377162873817647756142026806142597787926876264861380224440379927543802320580625784426007927955993320852739118400356448384366396329041614889063292067888366353036383063654754492436993137361121703178486219583117745209951459144854543144217112830979388614605805088577525947858099661844737381545915893113356524803295259661251464978316757787488376077844685558167255404535620027212670175512611302897522842178309043476982087953352305028940148319688670184979900065901326248807783611098051333540644550847570585295921219868494383960132354706526867722654073994632827811635833982528675681693122162264296479646868167451532626094165658151374368943293717618994082590595300167956594419075879793081978367883730780474672584089964561437266342743784511770459136509197136978309500601526453502259017792533495596964660043261271943895075142841483160040767563720144699907054266156661758811063241406677";

const count = {};
for (let i = 0; i <= 9; i++) {
    count[i] = 0;
}

for (const char of str) {
    if (/\d/.test(char)) {
        count[char]++;
    }
}

console.log("数字出现次数统计:");
for (const [digit, freq] of Object.entries(count)) {
    console.log(`${digit}: ${freq}次`);
}

运行效果:

 算对了,大模型有能力编写正确的代码,而且最重要的是,它有能力把参数一个字母不拉(顺序也不差)的放到代码中

例子2:排序

提示词:

将下列数字按照从小到大的顺序排序并输出:
28867 97366 80232 74608 41235 95097 91277 92328 84186 20723 60980 58788 86409 93851 33838 55261 8230 25139 87130 27468 14638 38424 27957 29535 64713 33169 19851 80361 71746 79797 865 41822 76762 66635 41517 9722 45763 52063 50981 16594 8328 32272 7098 70721 3820 82800 54676 10852 70495 18449 11219 90870 76921 68390 91432 29809 35242 76252 2898 17273 32751 60866 89469 35850 36590 42332 31662 3737 15509 99927 56739 73801 65893 77330 54123 35421 49158 90958 14948 22563 64160 40532 17404 65075 5709 89470 27625 21987 60147 30938 83458 29929 82112 63034 68075 14729 79094 96341 73894 42021 50641 74357 58225 76662 9348 84175 74476 446 9544 60595 91516 52655 80051 57240 42832 49375 30280 50106 24801 3334 14516 55603 31629 81433 65157 44235 30542 59781 36076 12919 26548 17637 49549 239 13759 38443 54061 43420 57129 53814 41025 13162 63903 25076 77254 92838 34585 23467 35245 23194 23142 57369 50496 38034 42479 58737 35539 24570 539 46690 86891 54975 10542 12175 24317 1444 28045 12552 22397 10638 53340 62372 22189 15651 32976 19671 76830 70639 70225 71058 19131 92073 92853 50527 56347 79960 40257 19954 1122 81747 41750 74200 18062 41995 80259 67285 89875 10164 54702 94964 

正确的答案:

[239, 446, 539, 865, 1122, 1444, 2898, 3334, 3737, 3820, 5709, 7098, 8230, 8328, 9348, 9544, 9722, 10164, 10542, 10638, 10852, 11219, 12175, 12552, 12919, 13162, 13759, 14516, 14638, 14729, 14948, 15509, 15651, 16594, 17273, 17404, 17637, 18062, 18449, 19131, 19671, 19851, 19954, 20723, 21987, 22189, 22397, 22563, 23142, 23194, 23467, 24317, 24570, 24801, 25076, 25139, 26548, 27468, 27625, 27957, 28045, 28867, 29535, 29809, 29929, 30280, 30542, 30938, 31629, 31662, 32272, 32751, 32976, 33169, 33838, 34585, 35242, 35245, 35421, 35539, 35850, 36076, 36590, 38034, 38424, 38443, 40257, 40532, 41025, 41235, 41517, 41750, 41822, 41995, 42021, 42332, 42479, 42832, 43420, 44235, 45763, 46690, 49158, 49375, 49549, 50106, 50496, 50527, 50641, 50981, 52063, 52655, 53340, 53814, 54061, 54123, 54676, 54702, 54975, 55261, 55603, 56347, 56739, 57129, 57240, 57369, 58225, 58737, 58788, 59781, 60147, 60595, 60866, 60980, 62372, 63034, 63903, 64160, 64713, 65075, 65157, 65893, 66635, 67285, 68075, 68390, 70225, 70495, 70639, 70721, 71058, 71746, 73801, 73894, 74200, 74357, 74476, 74608, 76252, 76662, 76762, 76830, 76921, 77254, 77330, 79094, 79797, 79960, 80051, 80232, 80259, 80361, 81433, 81747, 82112, 82800, 83458, 84175, 84186, 86409, 86891, 87130, 89469, 89470, 89875, 90870, 90958, 91277, 91432, 91516, 92073, 92328, 92838, 92853, 93851, 94964, 95097, 96341, 97366, 99927]

大语言模型的回答:

 可以看到从1444后面就开始错了

以下是大模型生成的验算的javascript代码:

const numbers = [
  28867, 97366, 80232, 74608, 41235, 95097, 91277, 92328, 84186, 20723, 60980, 58788, 86409, 93851, 33838, 55261, 8230, 25139, 87130, 27468, 14638, 38424, 27957, 29535, 64713, 33169, 19851, 80361, 71746, 79797, 865, 41822, 76762, 66635, 41517, 9722, 45763, 52063, 50981, 16594, 8328, 32272, 7098, 70721, 3820, 82800, 54676, 10852, 70495, 18449, 11219, 90870, 76921, 68390, 91432, 29809, 35242, 76252, 2898, 17273, 32751, 60866, 89469, 35850, 36590, 42332, 31662, 3737, 15509, 99927, 56739, 73801, 65893, 77330, 54123, 35421, 49158, 90958, 14948, 22563, 64160, 40532, 17404, 65075, 5709, 89470, 27625, 21987, 60147, 30938, 83458, 29929, 82112, 63034, 68075, 14729, 79094, 96341, 73894, 42021, 50641, 74357, 58225, 76662, 9348, 84175, 74476, 446, 9544, 60595, 91516, 52655, 80051, 57240, 42832, 49375, 30280, 50106, 24801, 3334, 14516, 55603, 31629, 81433, 65157, 44235, 30542, 59781, 36076, 12919, 26548, 17637, 49549, 239, 13759, 38443, 54061, 43420, 57129, 53814, 41025, 13162, 63903, 25076, 77254, 92838, 34585, 23467, 35245, 23194, 23142, 57369, 50496, 38034, 42479, 58737, 35539, 24570, 539, 46690, 86891, 54975, 10542, 12175, 24317, 1444, 28045, 12552, 22397, 10638, 53340, 62372, 22189, 15651, 32976, 19671, 76830, 70639, 70225, 71058, 19131, 92073, 92853, 50527, 56347, 79960, 40257, 19954, 1122, 81747, 41750, 74200, 18062, 41995, 80259, 67285, 89875, 10164, 54702, 94964
];

// 升序排序
const sorted = numbers.sort((a, b) => a - b); 

// 输出结果(每行10个数字格式化)
let output = '';
sorted.forEach((num, i) => {
  output += num + (i % 10 === 9 ? '\n' : ', ');
});
console.log(output.trim());

运行后的答案:

可以看到从1444后开始仍然是对的 

例子3:计算第x小值

提示词:

请找出下列数字中最小、第二小、第三小的数字:
875140993	912381640	8115283884	5481009296	9984984592	680345603	6556032372	4405284413	8438566434	7277128709	8690526074	1963368849	3813297428	7340099397	2345759580	8091736107	6582664730	7751765033	7858846991	3951099972	5979770141	4532078412	2368753609	6168537986	8187089341	6709318149	7705937946	1762145272	8995294359	70494063	9890031623	4984558723	4345744823	7512921379	6774618831	1353552741	3817106519	4788389342	5891049047	8915859798	3282305822	7658630835	9558557396	4566438486	6497111409	7329802310	8404895222	8111145007	5055967770	4439831214	1940443051	8545201444	6152466443	6560111111	4256720497	3642099460	3991908470	9662858390	5223055074	2484908155	8510102127	4858509408	7374299967	1770741920	5558313149	7646322758	6838733839	4333741642	6445215860	4966804280	1977819399	5053690960	1992410821	6818774218	683274425	1519195893	3897033621	8617121230	5847460944	5013977727	6536668387	1620799462	920607580	1139914966	4002972498	196186144	6465993760	2629645933	4377758695	3099981377	4504070302	5245280318	4230569216	9975276355	8047418595	9729607915	589140917	172451673	8038346280	2261157004	1666441152	4251246194	5638869178	9011902246	7374948392	2338140521	4115587227	3107238924	7492308088	9411170558	3924398660	7334192634	8821262926	9000086678	7620666843	6464816745	6032905905	5112494169	2822940847	5551789720	6003509036	8480725640	8162651520	7608021826	5787383354	3063388434	3466578853	5206006143	4627735589	8495488389	364769552	2993174582	5012378941	3124392458	5875044158	4493163465	852872884	5995391284	4781315237	4980335687	2845346704	7992328675	5288996046	4649704884	8516551704	6454820686	6528511550	3028336639	801362109	577348456	2717107800	4347501121	161573427	5753363629	7867743925	9338164870	3082995628	344142688	6428385447	6896187067	955329352	1152389165	5539846567	4624648103	5644606883	6429155148	4347149294	7063708768	2915920029	7415661736	9820389777	1505920853	335031381	503279921	2653669202	9020941806	5725048198	7491460061	7769760479	3653400958	7019242109	8476254514	5521954156	454878411	5963821370	3454875834	2648686248	5968903413	9071178594	2219702447	7802527172	1835529928	8953976840	950141596	2905918188	8364189413	2233511410	4185586054	6268500410	1972428546	

自己写程序计算的正确答案:

大模型算出的结果:

 

优化后的提示词: 

请编写可以找出下列数字中最小、第二小、第三小的数字的可以直接运行的 javascript 代码:
875140993	912381640	8115283884	5481009296	9984984592	680345603	6556032372	4405284413	8438566434	7277128709	8690526074	1963368849	3813297428	7340099397	2345759580	8091736107	6582664730	7751765033	7858846991	3951099972	5979770141	4532078412	2368753609	6168537986	8187089341	6709318149	7705937946	1762145272	8995294359	70494063	9890031623	4984558723	4345744823	7512921379	6774618831	1353552741	3817106519	4788389342	5891049047	8915859798	3282305822	7658630835	9558557396	4566438486	6497111409	7329802310	8404895222	8111145007	5055967770	4439831214	1940443051	8545201444	6152466443	6560111111	4256720497	3642099460	3991908470	9662858390	5223055074	2484908155	8510102127	4858509408	7374299967	1770741920	5558313149	7646322758	6838733839	4333741642	6445215860	4966804280	1977819399	5053690960	1992410821	6818774218	683274425	1519195893	3897033621	8617121230	5847460944	5013977727	6536668387	1620799462	920607580	1139914966	4002972498	196186144	6465993760	2629645933	4377758695	3099981377	4504070302	5245280318	4230569216	9975276355	8047418595	9729607915	589140917	172451673	8038346280	2261157004	1666441152	4251246194	5638869178	9011902246	7374948392	2338140521	4115587227	3107238924	7492308088	9411170558	3924398660	7334192634	8821262926	9000086678	7620666843	6464816745	6032905905	5112494169	2822940847	5551789720	6003509036	8480725640	8162651520	7608021826	5787383354	3063388434	3466578853	5206006143	4627735589	8495488389	364769552	2993174582	5012378941	3124392458	5875044158	4493163465	852872884	5995391284	4781315237	4980335687	2845346704	7992328675	5288996046	4649704884	8516551704	6454820686	6528511550	3028336639	801362109	577348456	2717107800	4347501121	161573427	5753363629	7867743925	9338164870	3082995628	344142688	6428385447	6896187067	955329352	1152389165	5539846567	4624648103	5644606883	6429155148	4347149294	7063708768	2915920029	7415661736	9820389777	1505920853	335031381	503279921	2653669202	9020941806	5725048198	7491460061	7769760479	3653400958	7019242109	8476254514	5521954156	454878411	5963821370	3454875834	2648686248	5968903413	9071178594	2219702447	7802527172	1835529928	8953976840	950141596	2905918188	8364189413	2233511410	4185586054	6268500410	1972428546

大模型生成的javascript 代码:


const numbers = `875140993	912381640	8115283884	5481009296	9984984592	680345603	6556032372	4405284413	8438566434	7277128709	8690526074	1963368849	3813297428	7340099397	2345759580	8091736107	6582664730	7751765033	7858846991	3951099972	5979770141	4532078412	2368753609	6168537986	8187089341	6709318149	7705937946	1762145272	8995294359	70494063	9890031623	4984558723	4345744823	7512921379	6774618831	1353552741	3817106519	4788389342	5891049047	8915859798	3282305822	7658630835	9558557396	4566438486	6497111409	7329802310	8404895222	8111145007	5055967770	4439831214	1940443051	8545201444	6152466443	6560111111	4256720497	3642099460	3991908470	9662858390	5223055074	2484908155	8510102127	4858509408	7374299967	1770741920	5558313149	7646322758	6838733839	4333741642	6445215860	4966804280	1977819399	5053690960	1992410821	6818774218	683274425	1519195893	3897033621	8617121230	5847460944	5013977727	6536668387	1620799462	920607580	1139914966	4002972498	196186144	6465993760	2629645933	4377758695	3099981377	4504070302	5245280318	4230569216	9975276355	8047418595	9729607915	589140917	172451673	8038346280	2261157004	1666441152	4251246194	5638869178	9011902246	7374948392	2338140521	4115587227	3107238924	7492308088	9411170558	3924398660	7334192634	8821262926	9000086678	7620666843	6464816745	6032905905	5112494169	2822940847	5551789720	6003509036	8480725640	8162651520	7608021826	5787383354	3063388434	3466578853	5206006143	4627735589	8495488389	364769552	2993174582	5012378941	3124392458	5875044158	4493163465	852872884	5995391284	4781315237	4980335687	2845346704	7992328675	5288996046	4649704884	8516551704	6454820686	6528511550	3028336639	801362109	577348456	2717107800	4347501121	161573427	5753363629	7867743925	9338164870	3082995628	344142688	6428385447	6896187067	955329352	1152389165	5539846567	4624648103	5644606883	6429155148	4347149294	7063708768	2915920029	7415661736	9820389777	1505920853	335031381	503279921	2653669202	9020941806	5725048198	7491460061	7769760479	3653400958	7019242109	8476254514	5521954156	454878411	5963821370	3454875834	2648686248	5968903413	9071178594	2219702447	7802527172	1835529928	8953976840	950141596	2905918188	8364189413	2233511410	4185586054	6268500410	1972428546`
    .split(/\s+/)
    .map(n => BigInt(n));

const sorted = [...numbers].sort((a, b) => a < b ? -1 : 1);
const [min, secondMin, thirdMin] = sorted.slice(0, 3);

console.log('最小数字:', min.toString());
console.log('第二小数字:', secondMin.toString());
console.log('第三小数字:', thirdMin.toString());

运行该 javascript 代码:

例子4:列表数据统计

提示词:

<<<
学生姓名	科目	分数
朱伟杰	数学	100
吴强芳	语文	147
何杰	生物	70
刘俊霞	数学	134
刘玉兰磊	化学	98
杨刚	语文	145
黄洋磊	语文	147
吴强芳	物理	89
何杰	物理	92
刘玉兰磊	数学	136
吴强芳	数学	139
王洋	生物	62
王洋	化学	89
王洋	英语	131
胡静	物理	110
李杰	生物	50
郭兰	物理	102
杨丽静	语文	140
郭洋娜	英语	135
马文	英语	138
胡静	语文	115
杨丽静	数学	106
郭艳	英语	148
李建华娟	英语	136
李建华娟	物理	63
杨丽静	英语	130
刘玉兰磊	生物	81
黄洋磊	化学	59
郭洋娜	物理	80
杨丽静	化学	76
刘俊霞	生物	57
黄洋磊	物理	108
郭刚	英语	115
郭兰	化学	100
胡静	化学	56
何杰	数学	147
胡静	英语	142
朱伟杰	化学	67
郭伟	语文	136
王强涛	英语	119
杨丽静	物理	83
郭兰	语文	112
杨刚	化学	57
郭刚	生物	75
郭艳	物理	107
何杰	化学	82
郭艳	化学	80
朱伟杰	英语	102
李杰	英语	107
李建华娟	生物	90
黄洋磊	数学	102
杨刚	数学	107
王洋	物理	71
刘玉兰磊	物理	88
郭洋娜	化学	71
郭刚	数学	127
郭艳	生物	67
马文	生物	53
王强涛	数学	109
郭伟	生物	80
郭洋娜	数学	133
李杰	化学	73
何杰	英语	132
郭伟	物理	104
刘玉兰磊	英语	147
郭洋娜	生物	83
刘玉兰磊	语文	103
刘俊霞	物理	94
郭兰	英语	131
胡静	生物	54
马文	语文	124
刘俊霞	化学	94
朱伟杰	物理	59
马文	物理	108
王洋	语文	115
马文	化学	66
吴强芳	生物	65
李杰	物理	73
郭艳	数学	150
胡静	数学	128
王洋	数学	121
郭伟	化学	77
郭伟	数学	118
王强涛	生物	74
吴强芳	化学	85
杨刚	物理	107
朱伟杰	生物	82
李杰	语文	114
郭洋娜	语文	121
郭兰	生物	57
李建华娟	语文	102
王强涛	物理	85
郭刚	化学	85
郭刚	语文	111
刘俊霞	英语	109
吴强芳	英语	127
郭兰	数学	148
刘俊霞	语文	135
杨刚	生物	56
杨丽静	生物	76
王强涛	化学	93
李建华娟	化学	66
马文	数学	116
郭伟	英语	134
何杰	语文	122
黄洋磊	英语	105
王强涛	语文	135
郭刚	物理	99
黄洋磊	生物	66
李建华娟	数学	117
杨刚	英语	145
朱伟杰	语文	111
郭艳	语文	147
李杰	数学	101
>>>
上述被 "<<<" 和 ">>>" 括起来的内容是某班级的学生各学科的分数列表信息,请找出总分数排名第一的学生姓名(如果存在多个并列第一的请输出多个学生的姓名)

正确答案:

 

大模型的回答(错误):

 大模型生成的验算 javascript 代码:

const data = `学生姓名	科目	分数
朱伟杰	数学	100
吴强芳	语文	147
何杰	生物	70
刘俊霞	数学	134
刘玉兰磊	化学	98
杨刚	语文	145
黄洋磊	语文	147
吴强芳	物理	89
何杰	物理	92
刘玉兰磊	数学	136
吴强芳	数学	139
王洋	生物	62
王洋	化学	89
王洋	英语	131
胡静	物理	110
李杰	生物	50
郭兰	物理	102
杨丽静	语文	140
郭洋娜	英语	135
马文	英语	138
胡静	语文	115
杨丽静	数学	106
郭艳	英语	148
李建华娟	英语	136
李建华娟	物理	63
杨丽静	英语	130
刘玉兰磊	生物	81
黄洋磊	化学	59
郭洋娜	物理	80
杨丽静	化学	76
刘俊霞	生物	57
黄洋磊	物理	108
郭刚	英语	115
郭兰	化学	100
胡静	化学	56
何杰	数学	147
胡静	英语	142
朱伟杰	化学	67
郭伟	语文	136
王强涛	英语	119
杨丽静	物理	83
郭兰	语文	112
杨刚	化学	57
郭刚	生物	75
郭艳	物理	107
何杰	化学	82
郭艳	化学	80
朱伟杰	英语	102
李杰	英语	107
李建华娟	生物	90
黄洋磊	数学	102
杨刚	数学	107
王洋	物理	71
刘玉兰磊	物理	88
郭洋娜	化学	71
郭刚	数学	127
郭艳	生物	67
马文	生物	53
王强涛	数学	109
郭伟	生物	80
郭洋娜	数学	133
李杰	化学	73
何杰	英语	132
郭伟	物理	104
刘玉兰磊	英语	147
郭洋娜	生物	83
刘玉兰磊	语文	103
刘俊霞	物理	94
郭兰	英语	131
胡静	生物	54
马文	语文	124
刘俊霞	化学	94
朱伟杰	物理	59
马文	物理	108
王洋	语文	115
马文	化学	66
吴强芳	生物	65
李杰	物理	73
郭艳	数学	150
胡静	数学	128
王洋	数学	121
郭伟	化学	77
郭伟	数学	118
王强涛	生物	74
吴强芳	化学	85
杨刚	物理	107
朱伟杰	生物	82
李杰	语文	114
郭洋娜	语文	121
郭兰	生物	57
李建华娟	语文	102
王强涛	物理	85
郭刚	化学	85
郭刚	语文	111
刘俊霞	英语	109
吴强芳	英语	127
郭兰	数学	148
刘俊霞	语文	135
杨刚	生物	56
杨丽静	生物	76
王强涛	化学	93
李建华娟	化学	66
马文	数学	116
郭伟	英语	134
何杰	语文	122
黄洋磊	英语	105
王强涛	语文	135
郭刚	物理	99
黄洋磊	生物	66
李建华娟	数学	117
杨刚	英语	145
朱伟杰	语文	111
郭艳	语文	147
李杰	数学	101`;

// 解析数据并计算总分
const students = {};
data.split('\n').slice(1).forEach(line => {
  const [name, subject, score] = line.split('\t');
  students[name] = (students[name] || 0) + parseInt(score);
});

// 找出最高分和对应的学生
const maxScore = Math.max(...Object.values(students));
const topStudents = Object.keys(students).filter(name => students[name] === maxScore);

console.log('总分排名第一的学生:');
topStudents.forEach(name => console.log(`${name} (总分: ${students[name]})`));

运行该代码算出了正确答案:

 发现分数随机的越有区分度(比如一些人考了10分、11分,而一些人考了140分、150分),大模型输出的越容易对,但是把分数的区分度降低(比如大家都考了1百多分),大模型很容易失败,这就更加应征了大模型根本不会思考和计算,就是纯概率输出,就像我们人的 "打眼一看",1万只羊群里混入了几只狗不用思考,一眼就能找到混入的几只狗,而当把5000只骡子和5000只驴混到一起就不好区分了,得仔细看

生成测试数据的java代码:

import com.alibaba.fastjson.JSONObject;
import java.util.*;

public class Test {


    public static void main(String[] args) {
        Set<String> studentNames = new HashSet<>();
        for (int i = 0; i < 20; i++) {
            String studentName = MyRandomUtils.generateRandomChineseUserName();
            studentNames.add( studentName );
        }
        StringBuilder sb = new StringBuilder("学生姓名\t科目\t分数\n");
        TreeMap<String, Long> map_studentName_totalScore = new TreeMap<>();
        List<StudentScore> studentScores = new ArrayList<>();
        for( String studentName:studentNames ){
           long chinaScore = MyRandomUtils.randomBetween(100L, 150L);
           long mathScore = MyRandomUtils.randomBetween(100L, 150L);
           long englishScore = MyRandomUtils.randomBetween(100L, 150L);
           long wuliScore = MyRandomUtils.randomBetween(50L, 110L);
           long huaxueScore = MyRandomUtils.randomBetween(50L, 100L);
           long shengwuScore = MyRandomUtils.randomBetween(50L, 90L);
            long totalScore = chinaScore + mathScore + englishScore + wuliScore + huaxueScore + shengwuScore;
            studentScores.add( StudentScore.build( studentName,"语文",chinaScore,totalScore ) );
            studentScores.add( StudentScore.build( studentName,"数学",mathScore,totalScore ) );
            studentScores.add( StudentScore.build( studentName,"英语",englishScore,totalScore ) );
            studentScores.add( StudentScore.build( studentName,"物理",wuliScore,totalScore ) );
            studentScores.add( StudentScore.build( studentName,"化学",huaxueScore,totalScore ) );
            studentScores.add( StudentScore.build( studentName,"生物",shengwuScore,totalScore ) );
            map_studentName_totalScore.put( totalScore + "_" + studentName,totalScore );
        }
        Collections.shuffle( studentScores );
        for( StudentScore studentScore:studentScores ){
            sb.append( studentScore.getStudentName() + "\t" + studentScore.getSubjectName() + "\t" + studentScore.getSubjectScore() + "\n" );
        }
        // 找出语文、数学、英语总分数排名第一的学生姓名(如果存在多个并列第一的请输出多个学生的姓名)
        System.out.println( sb.toString() );
        System.out.println(JSONObject.toJSONString( map_studentName_totalScore,true ));
    }
}
import lombok.Getter;
import lombok.Setter;
import java.io.Serializable;

@Getter
@Setter
public class StudentScore implements Serializable {
    private String studentName;
    private String subjectName;
    private long subjectScore;
    private long totalScore;

    public static StudentScore build(String studentName,
                                        String subjectName,
                                         long subjectScore,
                                         long totalScore ){
        StudentScore studentScore = new StudentScore();
        studentScore.setStudentName( studentName );
        studentScore.setSubjectName( subjectName );
        studentScore.setSubjectScore( subjectScore );
        studentScore.setTotalScore( totalScore );
        return studentScore;
    }
}
import java.security.SecureRandom;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.IntStream;

public class MyRandomUtils {

    private static final String LOWERCASE = "abcdefghijklmnopqrstuvwxyz";
    private static final String UPPERCASE = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    private static final String NUMBERS = "0123456789";
    private static final String UNDERSCORE = "_";
    private static final String ALL_CHARS = LOWERCASE + UPPERCASE + NUMBERS + UNDERSCORE;
    private static final SecureRandom random = new SecureRandom();

    public static void main(String[] args) {
        System.out.println( generateRandomPassword( 32 ) );
        System.out.println( generateRandomPassword( 64 ) );
    }

    /**
     * 生成32个字符长度的唯一的 String 类型 id
     * @return 例如:372a8a3d7b7ad8591831738890407247
     */
    public static String generateUniqueStringId(){
        String id = UUID.randomUUID().toString().replaceAll("-", "") + System.currentTimeMillis();
        id = id.substring( 13 );
        return id;
    }

    /**
     *
     * @param seperator
     * @return 例如:20250207/372a8a3d7b7ad8591831738890407247
     */
    public static String generateUniqueStringId_with_yyyyMMddPrefix( String seperator ){
        SimpleDateFormat sdf = MyDateUtils.buildSimpleDateFormat_timeZone_beijing("yyyyMMdd");
        String id = sdf.format(new Date()) + seperator + MyRandomUtils.generateUniqueStringId();
        return id;
    }


    /**
     * 生成指定长度的随机密码(包含数字、大小写字母、下划线)
     * @param len 密码的字符长度
     * @return
     */
    public static String generateRandomPassword( int len ){
        if (len <= 0) {
            throw new IllegalArgumentException("密码长度必须大于0");
        }
        List<Character> passwordChars = new ArrayList<>();
        // 确保每种字符类型至少出现一次
        passwordChars.add(LOWERCASE.charAt(random.nextInt(LOWERCASE.length())));
        passwordChars.add(UPPERCASE.charAt(random.nextInt(UPPERCASE.length())));
        passwordChars.add(NUMBERS.charAt(random.nextInt(NUMBERS.length())));
        passwordChars.add(UNDERSCORE.charAt(0));
        // 填充剩余长度
        for (int i = 4; i < len; i++) {
            passwordChars.add(ALL_CHARS.charAt(random.nextInt(ALL_CHARS.length())));
        }
        // 打乱顺序
        Collections.shuffle(passwordChars, random);
        // 构建结果字符串
        StringBuilder sb = new StringBuilder();
        for (char c : passwordChars) {
            sb.append(c);
        }
        return sb.toString();
    }

    public static String generateRandomSixDigitNumber( ) {
        // 创建一个Random对象
        Random random = new Random();
        // 生成一个随机的6位数字
        int randomNumber = 100000 + random.nextInt(900000);
        return String.valueOf( randomNumber );
    }

    /**
     * 生成指定位数的随机数
     * @param x 位数( 最小值:1,最大值:10 )
     * @return 随机数字符串
     */
    public static String generateRandomXDigitNumber_v1(int x) {
        if (x <= 0) {
            throw new IllegalArgumentException("位数必须大于0");
        }

        Random random = new Random();
        // 计算最小值和范围
        int min = (int) Math.pow(10, x - 1);
        int max = (int) Math.pow(10, x) - 1;

        // 生成随机数
        int randomNumber = min + random.nextInt(max - min + 1);
        return String.valueOf(randomNumber);
    }

    /**
     * 生成指定位数的随机数
     * @param x 位数(最 小值:1,最大值:Integer.MAX_VALUE )
     * @return 随机数字符串
     */
    public static String generateRandomXDigitNumber_v2(int x) {
        if (x <= 0) {
            throw new IllegalArgumentException("位数必须大于0");
        }
        Random random = new Random();
        StringBuilder sb = new StringBuilder(x);

        // 第一位1-9,保证位数准确
        sb.append(1 + random.nextInt(9));

        // 剩余位0-9
        IntStream.range(1, x).forEach(i -> sb.append(random.nextInt(10)));
        return sb.toString();
    }

    /**
     * 生成两个long值之间的随机数(包含边界值)
     * @param min 最小值(包含)
     * @param max 最大值(包含)
     * @return 介于min和max之间的随机long值
     * @throws IllegalArgumentException 如果min > max
     */
    public static long randomBetween(long min, long max) {
        if (min > max) {
            throw new IllegalArgumentException("min must be less than or equal to max");
        }

        Random random = new Random();
        // 处理边界情况
        if (min == max) {
            return min;
        }

        // 计算差值时考虑long溢出问题
        long difference = max - min;
        if (difference >= 0) {
            return min + Math.abs(random.nextLong() % (difference + 1));
        }

        // 处理差值溢出的特殊情况
        long randomValue;
        do {
            randomValue = random.nextLong();
        } while (randomValue < min || randomValue > max);

        return randomValue;
    }

    // 常见姓氏
    private static final String[] SURNAMES = {
            "王", "李", "张", "刘", "陈", "杨", "赵", "黄", "周", "吴",
            "徐", "孙", "胡", "朱", "高", "林", "何", "郭", "马", "罗"
    };

    // 常见名字用字(男女通用)
    private static final String[] NAME_CHARS = {
            "伟", "芳", "娜", "秀英", "敏", "静", "丽", "强", "磊", "军",
            "洋", "勇", "艳", "杰", "娟", "涛", "明", "超", "秀兰", "霞",
            "平", "刚", "桂英", "玉兰", "萍", "鹏", "建华", "俊", "文", "兰"
    };

    public static String generateRandomChineseUserName() {
        Random random = new Random();
        // 随机选择姓氏
        String surname = SURNAMES[random.nextInt(SURNAMES.length)];

        // 随机生成1-2个字的名字
        int nameLength = random.nextInt(2) + 1;
        StringBuilder name = new StringBuilder();
        for (int i = 0; i < nameLength; i++) {
            name.append(NAME_CHARS[random.nextInt(NAME_CHARS.length)]);
        }

        return surname + name.toString();
    }
}


网站公告

今日签到

点亮在社区的每一天
去签到