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