保证std :: shuffle返回唯一序列

时间:2018-12-01 18:12:28

标签: c++ c++17 shuffle

是否有办法保证lubridate产生的输出与输入不同?

下面是一个玩具示例,其中我在玩一些旧代码,这些代码会乱码单词中的字母,除了第一个和最后一个。我注意到对于短序列df <- structure(list(WEEK = structure(c(13885, 13885, 13892, 13892, 13892, 13892, 13899, 13899, 13906, 13906, 13913, 13913, 13920, 13927, 13927, 13927, 13934, 13941, 13941, 13941, 13948, 13948, 13955, 13955, 13962, 13962, 13962, 13976, 13976, 13983, 13997, 14004, 14004, 14011, 14018, 14018, 14025, 14025, 14032, 14032, 14039, 14039, 14046, 14053, 14060, 14067, 14067, 14067, 14081, 14081, 14081, 14081, 14081, 14081, 14088, 14088, 14095, 14102, 14102, 14109, 14109, 14109, 14116, 14116, 14123, 14137, 14137, 14137, 14137, 14137, 14144, 14144, 14144, 14151, 14151, 14151, 14158, 14165, 14165, 14165, 14165, 14172, 14172, 14172, 14172, 14172, 14179, 14186, 14186, 14186, 14193, 14193, 14200, 14207, 14207, 14207, 14214, 14214, 14214, 14214, 14228, 14228, 14228, 14235, 14235, 14242, 14242, 14242, 14242, 14242, 14249, 14249, 14256, 14263, 14270, 14284, 14291, 14291, 14298, 14305, 14305, 14312, 14319, 14319, 14319, 14333, 14333, 14333, 14333, 14340, 14340, 14347, 14347, 14354, 14354, 14361, 14361, 14361, 14361, 14368, 14375, 14375, 14375, 14375, 14382, 14389, 14389, 14396, 14403, 14410, 14410, 14417, 14424, 14424, 14431, 14431, 14438, 14445, 14466, 14466, 14473, 14480, 14487, 14487, 14487, 14487, 14494, 14494, 14501, 14501, 14501, 14501, 14515, 14515, 14515, 14515, 14522, 14522, 14522, 14522, 14522, 14529, 14529, 14529, 14536, 14536, 14543, 14543, 14543, 14550, 14550, 14550, 14550, 14557, 14557, 14564, 14564, 14564, 14564, 14571, 14571, 14578, 14578, 14578, 14585, 14592, 14592, 14592, 14592, 14599, 14599, 14599, 14613, 14613, 14613, 14613, 14620, 14620, 14620, 14620, 14620, 14620, 14620, 14620, 14627, 14641, 14641, 14641, 14641, 14655, 14655, 14683, 14683, 14690, 14690, 14697, 14697, 14711, 14711, 14711, 14718, 14718, 14718, 14725, 14725, 14732, 14739, 14739, 14739, 14746, 14746, 14760, 14767, 14774, 14774, 14781, 14781, 14781, 14788, 14795, 14795, 14795, 14795, 14795, 14802, 14809, 14809, 14809, 14816, 14823, 14830, 14830, 14830, 14837, 14844, 14844, 14851, 14851, 14851, 14851, 14851, 14851, 14858, 14865, 14865, 14872, 14872, 14872, 14872, 14872, 14879, 14879, 14886, 14886, 14886, 14893, 14893, 14893, 14893, 14900, 14907, 14907, 14907, 14907, 14907, 14914, 14914, 14921, 14921, 14921, 14928, 14935, 14942, 14942, 14949, 14956, 14956, 14956, 14970, 14977, 14984, 14984, 14984, 14984, 14991, 14991, 14991, 14998, 15005, 15005, 15005, 15012, 15012, 15019, 15019, 15019, 15019, 15026, 15026, 15033, 15033, 15040, 15040, 15040, 15040, 15040, 15047, 15054, 15061, 15068, 15068, 15068, 15075, 15075, 15075, 15096, 15096, 15096, 15103, 15103, 15103, 15103, 15103, 15103, 15103, 15103, 15110, 15110, 15110, 15117, 15117, 15124, 15131, 15131, 15131, 15131, 15138, 15145, 15145, 15145, 15159, 15166, 15166, 15166, 15173, 15173, 15180, 15180, 15194, 15194, 15194, 15194, 15201, 15201, 15201, 15208, 15208, 15208, 15215, 15222, 15222, 15222, 15229, 15229, 15243, 15243, 15250, 15250, 15257, 15257, 15264, 15271, 15278, 15278, 15278, 15278, 15285, 15285, 15292, 15292, 15292, 15299, 15299, 15313, 15327, 15327, 15327, 15341, 15341, 15341, 15348, 15355, 15362, 15362, 15362, 15369, 15369, 15376, 15383, 15383, 15390, 15397, 15404, 15404, 15411, 15411, 15418, 15418, 15418, 15425, 15425, 15425, 15432, 15439, 15439, 15439, 15439, 15446, 15446, 15446, 15446, 15453, 15460, 15460, 15467, 15467, 15467, 15474, 15481, 15488, 15509, 15509, 15516, 15523, 15523, 15523, 15523, 15537, 15544, 15551, 15565, 15565, 15572, 15572, 15579, 15579, 15586, 15593, 15600, 15600, 15600, 15607, 15607, 15607, 15614, 15614, 15621, 15628, 15628, 15635), class = "Date"), MINUTE = c(459L, 3609L, 3470L, 6347L, 6669L, 9330L, 2332L, 8263L, 4019L, 9390L, 2274L, 7954L, 532L, 6348L, 6563L, 7765L, 3857L, 3842L, 4123L, 9361L, 3711L, 5183L, 3805L, 5095L, 2238L, 2295L, 9280L, 839L, 2185L, 626L, 9538L, 6301L, 8242L, 2588L, 8226L, 9567L, 978L, 4013L, 2269L, 5371L, 2236L, 8447L, 9319L, 2337L, 3320L, 2527L, 6664L, 7868L, 1914L, 3983L, 4068L, 7940L, 9442L, 9721L, 2250L, 8031L, 9535L, 6671L, 6673L, 2240L, 4199L, 6549L, 1037L, 3910L, 3941L, 874L, 1042L, 2298L, 6380L, 8471L, 3744L, 5216L, 8253L, 2271L, 3616L, 9565L, 6589L, 3654L, 5216L, 5398L, 7857L, 3408L, 6469L, 6789L, 8221L, 8486L, 7823L, 6529L, 8123L, 9638L, 8111L, 8121L, 1015L, 2575L, 5251L, 9362L, 5364L, 8086L, 8256L, 9323L, 4941L, 6832L, 7939L, 994L, 3571L, 593L, 951L, 2221L, 3933L, 9402L, 2105L, 7888L, 6679L, 605L, 8241L, 5266L, 889L, 2488L, 5208L, 3930L, 7900L, 8164L, 3909L, 4077L, 8187L, 4858L, 5222L, 6302L, 7766L, 5267L, 5270L, 864L, 8176L, 3851L, 3855L, 1006L, 6431L, 8072L, 9631L, 5334L, 822L, 1851L, 5445L, 9371L, 6376L, 2555L, 6445L, 5005L, 1002L, 2383L, 6772L, 8080L, 4173L, 9359L, 703L, 8246L, 6796L, 6860L, 3673L, 9494L, 9492L, 2359L, 1058L, 5290L, 6442L, 8060L, 944L, 7419L, 5292L, 5574L, 6678L, 9260L, 979L, 2357L, 5495L, 7745L, 945L, 3842L, 6276L, 7927L, 9645L, 2433L, 6490L, 6815L, 9251L, 9433L, 2216L, 6902L, 9337L, 2491L, 3768L, 6418L, 6733L, 1041L, 3838L, 4008L, 6680L, 9272L, 9334L, 3861L, 6632L, 1176L, 8047L, 8181L, 7978L, 1135L, 3768L, 6318L, 9619L, 3646L, 3908L, 9657L, 977L, 2346L, 2349L, 6834L, 2129L, 3879L, 3974L, 4040L, 4951L, 6484L, 6692L, 8311L, 5430L, 6995L, 7981L, 9234L, 9420L, 9255L, 9449L, 3884L, 5352L, 3698L, 6585L, 756L, 1767L, 2453L, 2529L, 5208L, 1408L, 2684L, 5261L, 2389L, 6982L, 8184L, 2127L, 7994L, 9447L, 5423L, 7102L, 3925L, 3845L, 6394L, 9843L, 7842L, 8471L, 9295L, 4897L, 2087L, 2167L, 5321L, 8161L, 9708L, 9445L, 2609L, 4985L, 7847L, 6540L, 3559L, 8547L, 9488L, 9546L, 516L, 2214L, 6947L, 815L, 6732L, 8228L, 9311L, 9446L, 9891L, 7956L, 9242L, 9366L, 1078L, 2068L, 2403L, 3198L, 6731L, 2404L, 7856L, 6277L, 7845L, 9513L, 753L, 1987L, 2451L, 6692L, 3661L, 4010L, 6406L, 7701L, 9196L, 9379L, 1171L, 9258L, 3658L, 5081L, 9601L, 3863L, 9304L, 5455L, 6451L, 3626L, 5351L, 6818L, 8191L, 8123L, 5050L, 1824L, 5145L, 5368L, 6450L, 4980L, 6732L, 8003L, 5388L, 2342L, 3753L, 8317L, 3487L, 5733L, 2307L, 2505L, 5010L, 8189L, 5169L, 9354L, 7895L, 9638L, 948L, 3526L, 6707L, 6795L, 8164L, 3776L, 9482L, 8109L, 2058L, 9241L, 9313L, 3792L, 4945L, 5320L, 2108L, 3726L, 7944L, 724L, 3579L, 4932L, 5507L, 5541L, 7960L, 7987L, 8147L, 2061L, 2439L, 7784L, 3496L, 9562L, 751L, 6344L, 7786L, 7924L, 8028L, 9409L, 3680L, 6418L, 9651L, 6503L, 865L, 3684L, 5345L, 2405L, 3668L, 4020L, 5396L, 999L, 7940L, 9435L, 9570L, 515L, 596L, 3812L, 859L, 905L, 8302L, 8345L, 3745L, 5428L, 8108L, 4952L, 6780L, 2496L, 3204L, 4067L, 6929L, 2412L, 4950L, 9506L, 645L, 674L, 5231L, 5524L, 6579L, 3913L, 9569L, 2497L, 6476L, 9320L, 1112L, 2092L, 9372L, 957L, 2352L, 7851L, 598L, 747L, 6584L, 2021L, 3583L, 3562L, 5269L, 6634L, 6770L, 7716L, 7929L, 2076L, 4042L, 7952L, 3729L, 3535L, 6453L, 919L, 9748L, 1002L, 4027L, 5015L, 3800L, 3925L, 9559L, 4023L, 930L, 1000L, 6732L, 9289L, 2042L, 2457L, 6501L, 9672L, 2659L, 2458L, 8049L, 6368L, 8414L, 9889L, 6725L, 6978L, 919L, 2427L, 3702L, 2041L, 792L, 2426L, 2499L, 6710L, 705L, 2270L, 8071L, 1965L, 3784L, 579L, 8128L, 6767L, 6822L, 1153L, 9114L, 3874L, 6653L, 8136L, 3691L, 8465L, 9324L, 2106L, 7994L, 9477L, 4869L, 5225L, 580L)), row.names = c(NA, -500L), class = "data.frame") 会“随机地”将字母改回其原始位置并产生不希望的输出。因此,要“解决”该问题,我只需再次运行std::shuffle

用简单的话来说,有没有一种方法可以避免while循环,或者仅仅是std::shuffle的怪癖?

std::shuffle

1 个答案:

答案 0 :(得分:-1)

否。

std::shuffle正在按预期工作,并返回给定序列的随机排列。即使与输入相同。

重新滚动不需要的输出上的序列是最简单的解决方案,而且“反洗牌”无论如何都可能完成。