<루비>1000개 숫자 랜덤으로 생성하기

조회수 187회

1부터 1000까지 숫자를 랜덤으로 생성하고 싶은데.

a=[1..1000]

puts a.sort

하면 1..1000 이런식으로 밖에 안되던데... 1부터 1000까지 숫자 하나하나 다 생성하게 하려면 어떻게 해야될까요..ㅠ

1 답변

  • 정말 랜덤인건지

    1 ~ 1000개가 단 한번씩인데 섞여야하는지는 잘 모르겠네요.

    대략 아래처럼 하면 되지 않을까요?

    1. 랜덤일 경우

    a = 1000.times.map { rand(1000) }
    
    => [462, 262, 172, 57, 780, 797, 914, 116, 57, 138, 42, 667, 95, 752, 877, 995, 670, 609, 782, 282, 111, 285, 315, 482, 70, 69, 901, 519, 743, 624, 33, 240, 109, 794, 270, 934, 343, 677, 942, 397, 488, 921, 82, 134, 574, 780, 655, 527, 584, 920, 803, 722, 551, 152, 829, 986, 603, 529, 677, 988, 966, 609, 236, 65, 222, 133, 638, 610, 198, 745, 296, 184, 891, 165, 628, 910, 101, 70, 667, 917, 659, 937, 228, 794, 766, 821, 995, 37, 919, 191, 265, 769, 536, 907, 580, 284, 464, 917, 215, 545, 965, 924, 165, 831, 170, 581, 396, 256, 436, 779, 883, 333, 802, 263, 840, 719, 432, 171, 245, 679, 266, 276, 642, 24, 187, 366, 833, 0, 952, 478, 336, 230, 809, 295, 81, 653, 37, 483, 10, 29, 729, 29, 960, 283, 915, 522, 926, 829, 994, 103, 247, 9, 453, 65, 53, 225, 638, 5, 566, 897, 84, 873, 836, 722, 883, 888, 484, 371, 72, 297, 496, 351, 306, 653, 842, 572, 909, 435, 480, 276, 728, 458, 932, 683, 817, 99, 326, 560, 640, 574, 180, 494, 831, 341, 784, 460, 23, 402, 898, 33, 980, 698, 23, 107, 818, 905, 780, 268, 407, 224, 887, 954, 605, 793, 531, 946, 813, 561, 328, 951, 328, 211, 94, 969, 671, 980, 671, 892, 18, 396, 163, 504, 855, 393, 859, 994, 725, 343, 651, 881, 754, 792, 935, 195, 897, 992, 745, 622, 701, 157, 27, 603, 385, 151, 859, 617, 23, 169, 526, 1, 189, 388, 713, 154, 995, 709, 712, 631, 285, 673, 860, 123, 896, 158, 41, 906, 322, 233, 770, 131, 978, 541, 269, 954, 105, 923, 894, 445, 853, 290, 46, 947, 302, 962, 589, 221, 178, 883, 414, 890, 545, 22, 74, 242, 136, 467, 552, 536, 478, 473, 920, 375, 529, 721, 402, 896, 282, 272, 49, 690, 511, 45, 892, 148, 158, 716, 427, 632, 420, 558, 654, 692, 45, 681, 239, 97, 243, 247, 116, 224, 452, 22, 180, 194, 388, 686, 986, 429, 538, 546, 266, 982, 908, 176, 640, 470, 519, 999, 795, 559, 261, 413, 419, 365, 770, 125, 193, 472, 841, 756, 741, 316, 262, 471, 632, 434, 157, 442, 155, 383, 790, 985, 787, 374, 522, 11, 777, 585, 472, 67, 367, 713, 430, 700, 668, 588, 767, 354, 421, 542, 938, 872, 189, 85, 963, 460, 91, 896, 986, 784, 871, 136, 334, 934, 677, 353, 726, 48, 326, 832, 226, 259, 932, 436, 496, 38, 353, 536, 131, 243, 420, 243, 432, 876, 430, 715, 740, 314, 216, 156, 617, 909, 294, 434, 166, 999, 756, 943, 601, 66, 167, 577, 394, 323, 85, 329, 191, 224, 148, 955, 702, 446, 14, 364, 891, 222, 985, 37, 268, 370, 844, 275, 209, 42, 769, 174, 960, 190, 273, 235, 975, 444, 773, 301, 822, 61, 844, 632, 458, 263, 733, 283, 496, 254, 168, 495, 283, 445, 696, 703, 964, 538, 175, 639, 633, 993, 554, 727, 965, 916, 120, 894, 778, 646, 98, 423, 545, 724, 171, 801, 759, 540, 171, 768, 979, 693, 161, 79, 883, 561, 364, 676, 736, 260, 88, 518, 769, 775, 650, 441, 447, 883, 812, 51, 560, 558, 177, 687, 239, 396, 746, 280, 569, 18, 888, 176, 213, 267, 321, 184, 877, 786, 629, 183, 245, 244, 310, 883, 495, 808, 226, 693, 612, 267, 155, 380, 599, 423, 913, 928, 856, 858, 380, 844, 362, 563, 342, 72, 518, 480, 824, 554, 975, 654, 995, 884, 329, 717, 762, 182, 762, 671, 500, 342, 948, 242, 980, 73, 613, 773, 886, 677, 833, 363, 796, 39, 624, 285, 620, 190, 652, 175, 845, 271, 314, 832, 411, 117, 803, 601, 884, 930, 530, 280, 776, 370, 710, 730, 606, 794, 528, 835, 810, 426, 535, 306, 325, 523, 851, 945, 948, 700, 204, 146, 297, 982, 376, 985, 670, 307, 676, 647, 490, 230, 397, 777, 768, 804, 230, 528, 273, 27, 706, 2, 887, 491, 96, 859, 639, 234, 367, 728, 492, 952, 235, 703, 234, 538, 706, 288, 650, 120, 993, 108, 39, 639, 585, 938, 203, 858, 998, 184, 576, 29, 701, 814, 712, 757, 493, 458, 432, 447, 361, 537, 627, 966, 467, 149, 315, 466, 405, 580, 104, 136, 301, 0, 205, 423, 661, 541, 883, 49, 421, 850, 64, 294, 796, 709, 132, 398, 378, 497, 195, 447, 724, 667, 76, 175, 461, 675, 973, 360, 162, 520, 457, 212, 34, 879, 868, 463, 51, 411, 851, 621, 188, 672, 988, 980, 546, 621, 137, 579, 501, 90, 792, 295, 286, 244, 331, 225, 718, 538, 227, 812, 16, 881, 838, 333, 100, 803, 733, 618, 947, 457, 657, 690, 854, 975, 204, 267, 286, 760, 743, 142, 824, 825, 720, 70, 12, 517, 565, 939, 359, 566, 890, 373, 240, 705, 15, 837, 179, 172, 79, 106, 559, 458, 766, 866, 722, 248, 673, 382, 709, 731, 621, 510, 702, 31, 898, 446, 445, 273, 671, 610, 962, 68, 36, 15, 992, 652, 941, 472, 269, 795, 308, 524, 724, 740, 807, 283, 216, 308, 997, 673, 714, 51, 299, 1, 339, 234, 935, 889, 128, 13, 901, 944, 758, 21, 675, 603, 939, 534, 581, 49, 424, 532, 464, 390, 432, 802, 332, 398, 132, 596, 608, 241, 727, 853, 240, 573, 341, 612, 337, 979, 408, 694, 694, 254, 652, 304, 764, 316, 623, 403, 98, 73, 75, 952, 990, 312, 320, 275, 156, 911, 114, 127, 922, 540, 184, 874, 949, 137, 71, 383, 478, 376, 62, 963, 17, 474, 546, 159, 374, 236, 943, 918, 357, 224, 815, 413, 195, 962, 486, 834, 285, 889, 377, 609, 542, 309, 820, 510, 85, 115, 365, 162, 62, 57, 801, 224, 60, 696, 295, 164, 217, 842, 266, 864, 270, 159, 794, 115, 895, 703, 54, 343, 987, 81, 159, 717, 165, 104, 967, 790, 297, 631, 97, 471, 514, 312]
    

    2. 단 한번씩인데 섞여야 하는 경우

    a = [*1..1000].shuffle
    
    => [475, 153, 287, 173, 699, 281, 689, 350, 897, 977, 17, 235, 609, 987, 682, 924, 508, 762, 197, 945, 467, 380, 612, 178, 30, 436, 390, 526, 831, 378, 733, 396, 630, 15, 41, 830, 321, 83, 725, 408, 432, 412, 224, 304, 156, 544, 453, 616, 636, 602, 208, 426, 773, 66, 566, 329, 284, 476, 890, 747, 25, 870, 713, 402, 458, 836, 331, 807, 407, 792, 502, 23, 43, 937, 59, 946, 692, 449, 437, 202, 586, 542, 516, 466, 891, 257, 385, 139, 738, 787, 854, 164, 238, 316, 583, 968, 259, 282, 351, 707, 465, 305, 129, 394, 737, 862, 215, 306, 865, 34, 236, 377, 199, 803, 741, 399, 759, 501, 814, 816, 192, 214, 247, 144, 827, 47, 693, 834, 489, 905, 567, 965, 31, 478, 917, 52, 590, 442, 69, 829, 698, 175, 260, 749, 593, 469, 651, 418, 391, 65, 62, 119, 440, 295, 492, 218, 485, 925, 302, 482, 1000, 328, 537, 55, 346, 687, 619, 386, 950, 103, 522, 213, 798, 675, 114, 623, 813, 209, 933, 574, 565, 298, 74, 142, 731, 994, 11, 231, 265, 446, 716, 718, 416, 441, 420, 938, 461, 252, 872, 353, 54, 364, 642, 463, 928, 245, 18, 778, 520, 907, 841, 276, 848, 684, 901, 182, 632, 255, 728, 786, 683, 113, 403, 628, 344, 543, 219, 835, 246, 541, 250, 56, 151, 922, 855, 871, 927, 154, 708, 555, 654, 360, 997, 459, 766, 653, 349, 547, 769, 856, 634, 739, 42, 999, 227, 121, 205, 354, 323, 427, 410, 978, 996, 423, 89, 898, 832, 767, 993, 942, 184, 470, 979, 273, 985, 411, 920, 421, 582, 63, 951, 853, 186, 57, 992, 338, 678, 286, 278, 176, 597, 646, 148, 918, 514, 19, 665, 181, 777, 248, 92, 193, 359, 126, 789, 290, 345, 10, 428, 279, 592, 515, 975, 127, 384, 333, 560, 308, 490, 845, 78, 671, 549, 232, 200, 452, 735, 36, 782, 326, 241, 742, 576, 970, 392, 484, 964, 230, 317, 935, 51, 183, 64, 709, 82, 606, 348, 77, 761, 916, 28, 336, 136, 652, 764, 201, 196, 433, 102, 884, 472, 903, 141, 46, 611, 132, 517, 91, 297, 637, 381, 510, 457, 135, 686, 268, 806, 104, 450, 931, 705, 863, 58, 107, 133, 811, 404, 655, 371, 669, 879, 443, 892, 497, 387, 44, 40, 625, 155, 435, 730, 587, 301, 962, 960, 32, 48, 473, 540, 243, 483, 291, 73, 723, 958, 557, 190, 166, 661, 194, 21, 743, 152, 185, 9, 138, 895, 695, 468, 531, 751, 585, 659, 524, 128, 438, 237, 604, 843, 367, 991, 752, 989, 744, 800, 622, 627, 943, 327, 826, 719, 117, 877, 343, 556, 172, 676, 710, 98, 288, 819, 690, 499, 610, 900, 112, 226, 844, 165, 400, 763, 488, 677, 131, 217, 162, 130, 553, 914, 852, 75, 294, 451, 519, 880, 24, 607, 605, 748, 374, 753, 867, 94, 122, 963, 424, 788, 955, 174, 393, 310, 559, 775, 283, 657, 405, 754, 29, 552, 535, 507, 966, 558, 869, 899, 904, 948, 600, 342, 957, 810, 71, 271, 575, 150, 6, 513, 147, 439, 525, 372, 757, 562, 936, 419, 546, 722, 318, 888, 124, 170, 448, 242, 755, 462, 425, 923, 860, 837, 285, 572, 902, 868, 505, 253, 455, 293, 532, 595, 650, 263, 528, 579, 774, 563, 629, 603, 794, 736, 805, 596, 765, 770, 267, 983, 415, 821, 361, 912, 8, 672, 12, 388, 909, 833, 981, 885, 203, 220, 910, 80, 177, 973, 444, 887, 921, 486, 847, 498, 808, 337, 145, 325, 2, 577, 264, 795, 454, 934, 274, 160, 335, 822, 504, 663, 198, 464, 702, 266, 866, 706, 358, 20, 67, 529, 307, 857, 262, 915, 940, 980, 158, 4, 332, 70, 512, 858, 859, 746, 919, 729, 626, 431, 320, 726, 824, 340, 296, 375, 874, 697, 571, 818, 39, 673, 812, 721, 95, 123, 696, 793, 561, 379, 389, 969, 906, 584, 570, 783, 554, 45, 840, 700, 251, 137, 180, 303, 382, 447, 791, 932, 724, 109, 445, 356, 889, 14, 644, 534, 289, 163, 608, 311, 750, 780, 717, 930, 846, 398, 167, 93, 207, 240, 256, 275, 691, 536, 564, 756, 97, 620, 491, 961, 146, 188, 105, 976, 758, 362, 530, 802, 988, 339, 850, 548, 768, 817, 324, 319, 506, 330, 1, 785, 366, 272, 108, 668, 211, 106, 662, 221, 842, 876, 90, 422, 784, 908, 406, 223, 5, 239, 212, 300, 233, 414, 982, 929, 37, 509, 116, 120, 598, 801, 16, 487, 254, 50, 614, 13, 617, 471, 38, 503, 299, 189, 87, 157, 771, 954, 545, 967, 956, 434, 990, 881, 99, 49, 527, 631, 518, 495, 474, 685, 645, 479, 7, 258, 334, 680, 972, 796, 60, 551, 804, 111, 679, 409, 430, 79, 732, 573, 649, 292, 312, 873, 477, 984, 355, 368, 882, 140, 187, 720, 417, 234, 578, 875, 134, 779, 369, 939, 995, 820, 533, 86, 643, 413, 861, 947, 84, 365, 952, 460, 666, 594, 591, 496, 191, 615, 648, 22, 839, 269, 986, 640, 633, 523, 658, 53, 115, 809, 225, 228, 76, 481, 694, 244, 781, 277, 33, 790, 88, 959, 206, 370, 314, 35, 944, 727, 715, 998, 376, 159, 734, 149, 456, 511, 179, 68, 357, 772, 72, 581, 143, 828, 161, 635, 568, 893, 886, 125, 216, 397, 322, 210, 681, 974, 27, 313, 688, 383, 926, 660, 624, 101, 953, 913, 674, 740, 601, 81, 883, 760, 538, 949, 270, 799, 849, 96, 618, 521, 941, 500, 401, 3, 229, 171, 878, 118, 168, 280, 864, 639, 714, 776, 550, 347, 261, 911, 493, 249, 670, 971, 823, 494, 429, 712, 704, 195, 851, 641, 580, 315, 26, 711, 100, 638, 667, 204, 647, 569, 703, 896, 838, 825, 363, 664, 480, 745, 341, 599, 656, 61, 588, 373, 621, 309, 169, 539, 85, 894, 613, 395, 701, 110, 352, 222, 797, 589, 815]
    

답변을 하려면 로그인이 필요합니다.

Hashcode는 개발자들을 위한 무료 QnA 사이트입니다. 계정을 생성하셔야만 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)

ᕕ( ᐛ )ᕗ
로그인이 필요합니다

Hashcode는 개발자들을 위한 무료 QnA사이트 입니다. 계정을 생성하셔야만 글을 작성하실 수 있습니다.