驗證大語言模型不會算數但可以編寫算數的程序

摘要:

????????本文通過幾個實例測試了大語言模型在數學計算、排序、統計等方面的能力。結果顯示,對于簡單字符統計、排序等任務,大模型能正確生成實現代碼,但當數據區分度降低時容易出錯。在計算學生分數排名任務中,大模型生成的代碼能正確找出最高分學生,但人工驗證發現當分數相近時模型容易出錯,表明其本質是基于概率的"快速判斷"而非精確計算。文章還提供了用于生成測試數據的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-9IntStream.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();}
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/pingmian/89773.shtml
繁體地址,請注明出處:http://hk.pswp.cn/pingmian/89773.shtml
英文地址,請注明出處:http://en.pswp.cn/pingmian/89773.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

概率論與數理統計(八)

參數估計 通過取樣本&#xff0c;并用樣本構造函數&#xff0c;達成估計分布函數參數的目的 矩估計法 本質&#xff1a;用樣本的各階矩代替總體的各階矩&#xff0c;即取&#xff1a; E(X)X ̄1n∑iXiE(X2)1n∑iXi2E(X)\overline{X}\dfrac{1}{n}\sum_i X_i\\ E(X^2)\dfrac{1}…

服務器后臺崩潰的原因

當我們雙十一活動零點拼命刷新卻卡在支付完頁面&#xff0c;游戲頁面等不進去&#xff0c;公司系統癱瘓全體員工干瞪眼&#xff0c;服務器崩潰絕對是數字時代中的酷刑&#xff01;那服務器為什么會說崩就崩&#xff0c;用戶對于這種情況該如何進行避雷呢&#xff1f;服務器主要…

線程池與ThreadPoolExecutor源碼解析(上)

一、線程池線程池&#xff08;ThreadPool&#xff09;是一種線程復用的機制。它維護著若干個線程&#xff0c;任務來了就復用這些線程去執行&#xff0c;任務做完線程不會銷毀&#xff0c;而是回到池中等待下一個任務。為什么要用線程池&#xff1f;降低資源消耗&#xff1a;避…

Linux內核IP分片重組機制剖析:高效與安全的藝術

在IP網絡通信中,當數據包超過MTU限制時,路由器會將其拆分為多個分片。這些分片到達目標主機后,內核必須高效、安全地重組原始數據包。Linux內核的net/ipv4/inet_fragment.c實現了一套精妙的分片管理框架,完美平衡了性能和安全性需求。本文將深入剖析其設計哲學與關鍵技術。…

相機模型和對極幾何

一、相機模型 1.針孔相機模型-外參矩陣 1.世界坐標系到相機坐標系 世界坐標系&#xff1a;可以定義空間中任意一個位置&#xff0c;原點位置三個坐標軸方向坐標系姿態&#xff08;X,Y,Z&#xff09;相機坐標系&#xff1a;定義在相機上&#xff0c;原點是相機中心&#xff0c;z…

Git 常用命令與操作步驟

以下是 Git 常用命令與操作步驟 的整理&#xff0c;涵蓋日常開發中最核心的場景&#xff0c;適合快速查閱和上手&#xff1a;1. 初始化與克隆倉庫操作命令本地初始化倉庫git init克隆遠程倉庫git clone <倉庫URL> &#xff08;如 git clone https://gitlab.com/user/repo…

Leetcode-.283移動零

class Solution:def moveZeroes(self, nums: List[int]) -> None:"""Do not return anything, modify nums in-place instead."""pos0for i in range(len(nums)):if nums[i]!0:nums[pos],nums[i]nums[i],nums[pos]pos1本題運用雙指針來寫&…

在React中做過哪些性能優化?

1. 使用 React.memo 進行組件優化 問題:當父組件重新渲染時,子組件也會重新渲染,即使它的 props 沒有變化。 解決方案:使用 React.memo 包裹子組件,讓其只在 props 變化時才重新渲染。 const MyComponent = React.memo((props) => {// 子組件代碼 }); 2. 使用 useCa…

安裝docker可視化工具 Portainer中文版(ubuntu上演示,所有docker通用) 支持控制各種容器,容器操作簡單化 降低容器門檻

以下有免費的4090云主機提供ubuntu22.04系統的其他入門實踐操作 地址&#xff1a;星宇科技 | GPU服務器 高性能云主機 云服務器-登錄 相關兌換碼星宇社區---4090算力卡免費體驗、共享開發社區-CSDN博客 兌換碼要是過期了&#xff0c;可以私信我獲取最新兌換碼&#xff01;&a…

ansible批量部署zabbix客戶端

?ansible編寫劇本步驟 1??創建roles目錄結構2??在group_vars/all/main.yml中定義變量列表3??在tasks目錄下編寫tasks任務4??在files目錄下準備部署文件5??在templates目錄下創建j2模板文件6??在handlers目錄下編寫handlers7??在roles目錄下編寫主playbook8??運…

螞蟻數科AI數據產業基地正式投產,攜手蘇州推進AI產業落地

近日&#xff0c;螞蟻數科AI數據產業基地在太倉智匯谷科技創新園正式投產。該基地作為蘇州市首個AI數據產業基地&#xff0c;旨在通過跨行業人才與前沿技術&#xff0c;為長三角制造業、金融、醫療等領域的大模型落地提供場景化、高質量的訓練數據支撐。數據被視為AI學習的核心…

計算機的網絡體系及協議模型介紹

目錄 1、網絡協議介紹 1.1、定義 1.2、基本作用 1.3、協議的主要內容 2、網絡協議分層 2.1、協議分層原因 2.2、網絡協議分層的缺點 2.3、OSI協議和TCP/IP協議的聯系 3、TCP/IP 協議族 3.1、定義介紹 3.2、組成 1、應用層 2、運輸層 3、網絡層 3.3、底層流程 4、…

密碼管理安全防御

密碼管理是信息安全的核心環節,其目標是通過規范密碼的生成、存儲、傳輸、驗證和生命周期管理,防止未授權訪問,保護用戶賬號和系統資源的安全。以下從核心原則、技術實踐、常見問題及解決方案等方面詳細說明: 一、密碼管理的核心原則 密碼管理需遵循“安全性”與“可用性…

Java異步日志系統性能優化實踐指南:基于Log4j2異步Appender與Disruptor

Java異步日志系統性能優化實踐指南&#xff1a;基于Log4j2異步Appender與Disruptor 一、技術背景與應用場景 在高并發的后端應用中&#xff0c;日志記錄往往成為性能瓶頸之一。同步寫日志會阻塞業務線程&#xff0c;導致響應延遲&#xff1b;而簡單的異步隊列實現又可能出現積壓…

Mybatis07-緩存

一、緩存機制的原理計算機每次從mysql中執行sql語句&#xff0c;都是內存與硬盤的通信&#xff0c;對計算機來說&#xff0c;影響效率。因此使用緩存機制。1-1、MyBatis 的緩存機制&#xff1a;執行 DQL&#xff08;select 語句&#xff09;的時候&#xff0c;將查詢結果放到緩…

【機器學習深度學習】LoRA 與 QLoRA:大模型高效微調的進階指南

目錄 前言 一、LoRA&#xff1a;低秩微調的經典之作 二、QLoRA&#xff1a;效率與精度的升級版 三、LoRA vs QLoRA&#xff1a;如何選擇&#xff1f; 3.1 性能維度對比 3.2 根據「顯卡資源」選擇 3.3 根據「任務類型與目標」選擇 3.4 根據「模型規模」選擇 3.5 根據…

教育行業網絡升級最佳實踐:SD-WAN、傳統方案與混合方案對比分析

隨著教育行業的數字化轉型不斷深入&#xff0c;網絡的穩定性、靈活性和安全性成為各類教育應用&#xff08;如遠程課堂、智慧校園和教育云平臺&#xff09;的核心支撐。然而&#xff0c;傳統的 MPLS 專線方案成本高、擴展性差&#xff0c;而純 SD-WAN 的方案在極高可靠性要求的…

[黑馬頭條]-文章列表加載

目錄 1.1)需求分析 1.2)表結構分析 ap_article 文章基本信息表 ap_article_config 文章配置表 ap_article_content 文章內容表 導入文章數據庫 實現思路 接口定義 功能實現 定義接口 編寫mapper文件 編寫業務層代碼 實現類&#xff1a; 定義常量類 編寫控制器代碼 …

使用TIANAI-CAPTCHA進行行為驗證碼的生成和緩存的二次校驗

1.導入依賴&#xff1a;<dependency><groupId>cloud.tianai.captcha</groupId><artifactId>tianai-captcha-springboot-starter</artifactId><version>1.5.2</version> </dependency>2.在application.yml中配置驗證碼相關配置…

db.refresh()的重復使用和db.rollback()

db.refresh()在 SQLAlchemy 中&#xff0c;db.refresh() 用于從數據庫中重新加載對象的狀態&#xff0c;確保對象屬性與數據庫中的實際數據保持一致。下面詳細介紹其使用場景和作用&#xff1a;1.獲取數據庫生成的值當數據庫自動生成字段&#xff08;如自增 ID、默認值、觸發器…