-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cpp
More file actions
1638 lines (1435 loc) · 40.6 KB
/
Copy pathtest.cpp
File metadata and controls
1638 lines (1435 loc) · 40.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <string>
#include <sstream>
#include <map>
#include <vector>
#include <algorithm>
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <stdint.h>
//open
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
//time
#include <sys/time.h>
#include <time.h>
//boost random
#include <boost/random.hpp>
#include "random.h"
#include "bitop.h"
#include <errno.h>
using namespace std;
int test_bitshift(int argc, char* argv[])
{
printf("1<<0 == %#x\n", 1<<0);
printf("1<<1 == %#x\n", 1<<1);
return 0;
}
class A
{
public:
A(int a1)
{
a = a1;
std::cout<<"Con A with para:"<<a1<<","<<this<<endl;
}
A()
{
a = 0;
std::cout<<"Con A with null"<<","<<this<<endl;
}
A(const A& that)
{
a = that.a;
std::cout<<"Con A with ref:"<<that.a<<",this:"<<this<<",that:"<<&that<<endl;
}
~A()
{
std::cout<<"deCon A:"<<a<<","<<this<<endl;
}
A& operator=(A &that)
{
std::cout << "=," <<"this:"<<this<<",that:"<<&that<<endl;
a = that.a;
}
int a;
};
int funcref(A &a)
{
cout<<"in funcref: &a:"<<&a<<endl;
}
int test_ref(int argc , char *argv[])
{
A a1(100);
cout<<"in test_ref: a1:"<<&a1<<endl;
funcref(a1);
}
int test_map(int argc, char* argv[])
{
map<int, A> mapa;
cout<<"check a"<<endl;
A a1(10);
/*
mapa.insert(pair<int, A>(1, a1));
*/
A &a2=a1;
cout <<"&a1:"<<&a1<<",&a2:"<<&a2<<endl;
vector<A> vec_a;
vec_a.push_back(a1);
a1.a=20;
cout<<"after change:"<<vec_a[0].a<<endl;
return 0;
}
map<int, A> mappa;
int test_map_deconstruct(int argc, char *argv[])
{
A a(1);
mappa[1]= a;
return 0;
}
#include <openssl/md5.h>
unsigned get_hash_key(const char *d, unsigned l)
{
static unsigned char buf[16];
MD5((const unsigned char *)d, l, buf);
unsigned u;
memcpy(&u, buf + 12, 4);
return u;
}
int hex2str(const string &hex, string &result)
{
if (hex.size() % 2 != 0) return -1;
result = "";
char c;
unsigned u;
for (unsigned i = 0; i < hex.size() / 2; ++i)
{
sscanf(hex.c_str() + i * 2, "%2x", &u);
c = u;
result.append(&c, 1);
}
return 0;
}
std::string str2hex(const char* key, unsigned len)
{
unsigned bucketnum = get_hash_key(key, len);
char hex[1024]={0};
char buffer[8]={0};
for(unsigned n=0; n < len ; ++n) {
sprintf(buffer, "%02x", (unsigned char)key[n]);
hex[2*n]=buffer[0];
hex[2*n+1]=buffer[1];
}
hex[len*2]='\0';
char buf[1024] = {0};
snprintf(buf, 1024, "bucket:%u: hex:%s", bucketnum, hex);
return std::string(buf);
}
int test_hex2str(int argc, char *argv[])
{
string hex = argv[1];
string result;
hex2str(hex, result);
cout<<result<<endl;
return 0;
}
int test_str2hex(int argc, char *argv[])
{
string str = argv[1];
string result = str2hex(str.c_str(), str.size());
cout<<result<<endl;
return 0;
}
int test_strerror(int argc, char *argv[])
{
int errorno = atoi(argv[1]);
string result = strerror(errorno);
cout<<result<<endl;
}
int test_rand(int argc, char *argv[])
{
printf("rand_max:%d. ", RAND_MAX);
for (int i=0;i<100;i++)
{
int r = rand();
printf("%10d ", r);
}
printf("\n");
return 0;
}
void get_start_num(int total, int start, int num, int &real_start, int &real_num)
{// [start, end]
if (start < 0) // 转成正的start
{
int end = total + start;
start = end - num + 1;
if (start < 0 && end < 0) // 越界了
{
real_num = 0;
real_start = 0;
}
else
{
if (start < 0) start = 0;
if (end < 0) end = 0;
real_start = start;
real_num = end - start + 1;
}
}
else
{
if (start > total - 1) // 越界了
{
real_num = 0;
real_start = 0;
}
else
{
int end = start + num - 1;
if (end > total - 1) end = total - 1;
real_start = start;
real_num = end - start + 1;
}
}
}
int test_get_start_num(int argc, char* argv[])
{
if (argc<3)
{
printf("param error\n");
return -1;
}
int total = atoi(argv[1]);
int start = atoi(argv[2]);
int num = atoi(argv[3]);
int r_start = -1;
int r_num = -1;
get_start_num(total, start, num, r_start, r_num);
printf("total:%d, start:%d, num:%d, r_start:%d, r_num:%d\n",
total, start, num, r_start, r_num);
return 0;
}
//double CCreateRedPacketMineSweep::GetRate(int portion, int coin, GoGirlIntList &datalist)
double gen_redpacket(int portion, int coin, vector<int> &vecnums)
{
int inc = 0;
// DEBUG_P(LOG_DEBUG, "CCreateRedPacketMineSweep::GetRate: func begin\n");
int left = coin - portion;
int tmpportion = portion;
int external_val = 1; //每个人预留coin
while (1)
{
if (portion <= 0)
{
break;
}
int result = (left - inc) / (portion);
// AsnInt *p = _cur_red_packet.coinlist.Append();
int this_redpacket = 0;
if (result < 1)
{
// DEBUG_P(LOG_DEBUG, "CCreateRedPacketMineSweep::GetRate: func begin111 coin=%d portion=%d\n", coin, portion);
if (left - inc < 0)
{
this_redpacket = 0;
}
else
{
int tmpdata = ((left - inc) * 100) / tmpportion;
int rnd = rand() % 100;
if (rnd < tmpdata)
{
this_redpacket = 1;
inc += 1;
}
else
{
this_redpacket = 0;
}
// DEBUG_P(LOG_DEBUG, "CCreateRedPacketMineSweep::GetRate: func begin111 coin=%d portion=%d tmpdata=%d rnd=%d *p=%d inc=%d\n",
// coin, portion, tmpdata, rnd, (int)*p, inc);
}
}
else
{
// DEBUG_P(LOG_DEBUG, "CCreateRedPacketMineSweep::GetRate: func begin222\n");
int large = ((left - inc) / (portion)) * 2;
int mod = 0;
if (large > 0)
{
mod = 1 + rand() % large;
}
else
{
mod = 0;
}
inc += mod;
this_redpacket = mod;
// DEBUG_P(LOG_DEBUG, "CCreateRedPacketMineSweep::GetRate: coin=%d inc=%d portion=%d large=%d mod=%d\n", coin, inc, portion, large, mod);
}
this_redpacket = this_redpacket + external_val;
vecnums.push_back(this_redpacket);
portion--;
}
return 1.0;
}
int gen_redpacket_seq_excl(int portion, int coin, int excl_bmp, int factor, vector<int> &vecnums)
{
// portion min is 1
// rand range: [0, coin - portion]
int r_portion = portion;
int left_coin = coin;
int this_portion = 0;
// 分数不能大于金额
if (portion > coin)
{
return 10;
}
//红包最大因子不能超过份数
if ((factor >= portion*100) || (factor < 100))
{
return 11;
}
//不能排除全部的尾数
excl_bmp &= 0x3FF;
if (excl_bmp == 0x3FF )
{
return 12;
}
int rsv_factor = (factor + 100)/100;
bool lastportion = false;
for (int i=0; i < portion; i++)
{
if (left_coin < r_portion )
{
fprintf(stderr, "left_coin <= r_portion, left_coin = %d, r_portion:%d\n", left_coin, r_portion);
return 20;
}
int avg_portion = left_coin / r_portion;
int max_portion = (factor * avg_portion)/100;
if (max_portion == 0) max_portion = 1;
if (i == portion - 1)
{
this_portion = left_coin;
lastportion = true;
}
else
{
int mod_factor = left_coin - r_portion * rsv_factor;
if (mod_factor <= 0) mod_factor = 1;
mod_factor = (mod_factor < max_portion)?mod_factor:max_portion;
int r = rand() % mod_factor;
this_portion = (r==0)?1:r;
}
//尾数在排除列表中,则调整之: 随机找一个不被排除的尾数
int tail = this_portion % 10;
if (test_bit(excl_bmp, tail))
{
int adj_portion = 0;
int b_digit = rand()%10;
int aj_tail = b_digit;
for (int j=b_digit; j<b_digit+10; j++)
{
aj_tail = j % 10;
adj_portion = (this_portion / 10)*10 + aj_tail;
if (!test_bit(excl_bmp, aj_tail) && (adj_portion > 0))
{
break;
}
}
//最后一份的调整,还必须反向调整,在已经生成的序列里挑一个,
//满足两个条件: 不是雷 + 总和不变
//一次调整失败,则继续尝试剩下的尾数
if (lastportion)
{
bool adj_ok = false;
int aj_end = aj_tail + 10;
for(int i=aj_tail;i<aj_end;i++)
{
aj_tail = i % 10;
adj_portion = (this_portion / 10)*10 + aj_tail;
if (test_bit(excl_bmp, aj_tail) || (adj_portion <= 0))
{
continue;
}
int diff = tail - aj_tail;
for(vector<int>::iterator it = vecnums.begin();it!=vecnums.end();it++)
{
int p = *it;
p += diff;
int ptail = p % 10;
if (!test_bit(excl_bmp, ptail) && (p>0) )
{
*it = p;
adj_ok = true;
goto adjfi;
}
}
}
if (!adj_ok)
{
fprintf(stderr, "adjust last portion failed!\n");
return 30;
}
}
adjfi:
this_portion = adj_portion;
}
r_portion --;
vecnums.push_back(this_portion);
left_coin -= this_portion;
}
return 0;
}
/*
*
* minenum_rate[]:雷数概率 0,1,2, ..., portionnum
* tailbmp: 尾数位图
* portion_num: 红包份数
* coin: 红包金额
* factor: 最大红包因子 , 取值范围[100, portion_num*100)
* lucky_num: 幸运数
*/
int gen_redpacket_seq_by_tail_mine(int minenum_rate[], int tailbmp, int lucky_num, int portion_num, int coin, int factor, vector<int> &vecnums)
{
//tailbmp 至少要制定一个尾数
if ((tailbmp | ((1<<portion_num)-1)) == 0)
{
fprintf(stdout, "tailbmp is all zero");
return 1;
}
// 分数不能大于金额
if (portion_num > coin)
{
fprintf(stdout, "portion_num[%d] invalid, should less than coin[%d].\n", portion_num, coin);
return 10;
}
//红包最大因子不能超过份数
if ((factor >= portion_num*100) || (factor < 100))
{
printf("factor[%d] invalid, the correct range is [100, portion_num*1000).\n", factor);
return 11;
}
int left_coin = coin;
int left_portion_num = portion_num;
int avg_portion = (left_coin+left_portion_num) / left_portion_num;
int max_portion = (factor * avg_portion)/100;
if (max_portion == 0) max_portion = 1;
//概率检查, 雷数概率检查, 概率之和应该等于1000
int sum_check = 0;
for (int i=0;i<portion_num+1;i++)
{
sum_check += minenum_rate[i];
}
if (sum_check != 1000)
{
fprintf(stderr, "minenum_rate invalid. sum is not eqaul 1000\n");
return 1;
}
//确定雷数
int left_ceil = 0;
int r = rand() % 1000;
int tail_rep_num = 0;
for (;tail_rep_num<portion_num+1; tail_rep_num ++)
{
int right_ceil = left_ceil + minenum_rate[tail_rep_num];
if (r < right_ceil)
{
break;
}
left_ceil = right_ceil;
}
if ((tail_rep_num > 0) && (lucky_num>0))
{
if (test_bit(tailbmp, lucky_num % 10))
{
tail_rep_num --;
}
}
//生成包含尾数的红包,n
vector<int> vec_rep_portions;
if (lucky_num > 0)
{
vec_rep_portions.push_back(lucky_num);
left_coin -= lucky_num;
left_portion_num --;
}
int trytimes=3*tail_rep_num;
for (;;)
{
//确定尾数
int r = rand();
int tail_digit = 0;
for (int i = r%10;i<r+10;i++)
{
int tail_bit=i%10;
if (test_bit(tailbmp, tail_bit) )
{
tail_digit = tail_bit;
break;
}
}
r = r % max_portion + 1;
r = (r/10)*10+tail_digit;
//判断是否重复, 重复则重试生成
bool isrep = false;
for(vector<int>::iterator it = vec_rep_portions.begin();it!=vec_rep_portions.end();it++)
{
int num = *it;
if (num == r)
{
isrep = true;
break;
}
}
if (isrep || r == 0)
{
trytimes --;
if (trytimes == 0)
{
fprintf(stdout, "gen mine failed!!");
return 11;
}
else
{
continue;
}
}
left_coin -= r;
left_portion_num --;
vec_rep_portions.push_back(r);
tail_rep_num --;
if (tail_rep_num <=0)
{
break;
}
}
/* DEBUG
char seqstr[100]={0};
int strn = 0;
for(vector<int>::iterator it = vec_rep_portions.begin();it!=vec_rep_portions.end();it++)
{
strn += snprintf(seqstr+strn, 100-strn, " %d ", *it);
}
printf("left_portion_num:%d, left_coin:%d, tailbmp:%#x, factor:%d, rep_portions:%s\n"
, left_portion_num, left_coin, tailbmp, factor, seqstr);
*/
//生成剩下的份额
int retry = 2;
vector<int> vec_leftnums;
retry:
vec_leftnums.clear();
int ret = gen_redpacket_seq_excl(left_portion_num, left_coin, tailbmp, factor, vec_leftnums);
if (ret)
{
if (retry)
{
goto retry;
retry --;
}
return ret;
}
//合并结果
int rep_pos = 0;
int left_pos = 0;
for(int i=0;i<portion_num;i++)
{
int p = -1;
if (rep_pos < vec_rep_portions.size() && left_pos < vec_leftnums.size())
{
if (rand()%2)
{
p = vec_rep_portions[rep_pos];
rep_pos ++;
}
else
{
p = vec_leftnums[left_pos];
left_pos ++;
}
}
else if (rep_pos < vec_rep_portions.size())
{
p = vec_rep_portions[rep_pos];
rep_pos ++;
}
else /* (left_pos == vec_leftnums.size()) */
{
p = vec_leftnums[left_pos];
left_pos ++;
}
vecnums.push_back(p);
}
random_shuffle(vecnums.begin(), vecnums.end());
/*
vecnums.reserve(vec_leftnums.size() + vec_rep_portions.size());
vecnums.insert(vecnums.end(), vec_leftnums.begin(), vec_leftnums.end());
vecnums.insert(vecnums.end(), vec_rep_portions.begin(), vec_rep_portions.end());
*/
return 0;
}
int test_gen_redpacket_seq_by_tail_mine(int argc, char* argv[])
{
int ret = 0;
int minenum_rate[11] = {
/*0*/500, /*1*/500, /*2*/0, /*3*/0, /*4*/0
,/*5*/0, /*6*/0, /*7*/0, /*8*/0, /*9*/0, /*10*/0
};
char tailstr[32]={0};
char seqstr[100]={0};
int strn = 0;
int test_times = 10000000;
int portion = 5;
int coin = 5000;
//int lucky_num = 2017;
int lucky_num = 0;
int tailarr[2] = {1,7};
vector<int> vectail(tailarr, tailarr + sizeof(tailarr)/sizeof(int));
int tailbmp = 0;
for(vector<int>::iterator it = vectail.begin();it!=vectail.end();it++)
{
int tail = *it;
tailbmp |= (1<<tail);
strn += snprintf(tailstr+strn, 32-strn, "%d_", tail);
}
int factor = 217;
vector<int> vecnums;
srand(1703232754+time(NULL));
int ok_times=0;
int errtimes = 0;
int errtimes_sum = 0;
int errtimes_zero = 0;
int errtimes_lucky = 0;
int errtimes_portion = 0;
for(int i=0;i<test_times;i++)
{
vecnums.clear();
ret = gen_redpacket_seq_by_tail_mine(minenum_rate, tailbmp, lucky_num, portion, coin, factor, vecnums);
if (ret)
{
fprintf(stderr, "error. ret:%d\n", ret);
continue;
//return ret;
}
bool ok = true;
bool lucked = false;
int sum = 0;
int tail_num = 0;
int zero_num = 0;
strn = 0;
bzero(seqstr, 100);
for(vector<int>::iterator it = vecnums.begin();it != vecnums.end();it++)
{
int p = *it;
sum += p;
int p_tail = p % 10;
if (p == lucky_num)
{
lucked = true;
}
for(vector<int>::iterator it = vectail.begin();it!=vectail.end();it++)
{
int tail = *it;
if (tail == p_tail)
{
//fprintf(stdout, "hit tail!portion:%d, tail:%d\n", portion, tail);
tail_num ++;
}
}
if (p == 0)
{
zero_num++;
}
strn += snprintf(seqstr+strn, 100-strn, "%d_", p);
}
if (vecnums.size()!=portion) { ok = false; errtimes_portion ++; }
if (lucky_num > 0 && !lucked ) { ok = false; errtimes_lucky ++; }
if (zero_num) { ok = false; errtimes_zero ++; }
if (sum-coin) { ok = false; errtimes_sum ++; }
if (!ok)
{
errtimes++;
fprintf(stderr, "nok. lucky_num:%d, tailstr:%s, tail_num:%d, zero_num:%d, sum:%d, coin:%d, hasluck:%d, portion:%d, seqlen:%d, seqlist:%s\n"
,lucky_num, tailstr ,tail_num, zero_num, sum, coin, lucked, portion, vecnums.size(), seqstr);
continue;
}
printf("%s\n", seqstr);
ok_times ++;
}
fprintf(stdout, "coin:%d, portion:%d, tailbmp:%#x, factor:%d, test_times:%d, ok_times:%d, errtimes:%d, portion_err:%d, lucky_err:%d, zero_err:%d, sum_err:%d\n",
coin, portion, tailbmp, factor, test_times, ok_times, errtimes, errtimes_portion, errtimes_lucky, errtimes_zero, errtimes_sum);
return 0;
}
int test_gen_redpacket_seq_excl(int argc, char* argv[])
{
int ret = 0;
char tailstr[32]={0};
char seqstr[100]={0};
int strn = 0;
int test_times = 10000000;
int portion = 10;
int coin = 1000;
int tailarr[2] = {1,7};
vector<int> vectail(tailarr, tailarr + sizeof(tailarr)/sizeof(int));
int excl_bmp = 0;
for(vector<int>::iterator it = vectail.begin();it!=vectail.end();it++)
{
int tail = *it;
excl_bmp |= (1<<tail);
strn += snprintf(tailstr+strn, 32-strn, "%d_", tail);
}
int factor = 217;
vector<int> vecnums;
srand(1703232754+time(NULL));
int ok_times=0;
for(int i=0;i<test_times;i++)
{
vecnums.clear();
ret = gen_redpacket_seq_excl(portion, coin, excl_bmp, factor, vecnums);
if (ret)
{
fprintf(stderr, "error. ret:%d\n", ret);
continue;
//return ret;
}
int sum = 0;
int tail_num = 0;
int zero_num = 0;
strn = 0;
bzero(seqstr, 100);
for(vector<int>::iterator it = vecnums.begin();it != vecnums.end();it++)
{
int portion = *it;
sum += portion;
int portion_tail = portion % 10;
for(vector<int>::iterator it = vectail.begin();it!=vectail.end();it++)
{
int tail = *it;
if (tail == portion_tail)
{
fprintf(stdout, "hit tail!portion:%d, tail:%d\n", portion, tail);
tail_num ++;
}
}
if (portion == 0)
{
zero_num++;
}
strn += snprintf(seqstr+strn, 100-strn, "%d_", portion);
}
if (tail_num || zero_num || (sum-coin))
{
fprintf(stderr, "excl_tail:%s, tail_num:%d, zero_num:%d, sum:%d, coin:%d, seqlist:%s\n"
,tailstr ,tail_num, zero_num, sum, coin, seqstr);
continue;
}
printf("%s\n", seqstr);
ok_times ++;
}
fprintf(stdout, "coin:%d, portion:%d, excl_bmp:%#x, factor:%d, test_times:%d, ok_times:%d\n",
coin, portion, excl_bmp, factor, test_times, ok_times);
}
int gen_redpacket1(int portion, int coin, int factor, vector<int> &vecnums)
{
// portion min is 1
// rand range: [0, coin - portion]
int r_portion = portion;
int left_coin = coin;
int this_portion = 0;
// 分数不能大于金额
if (portion > coin)
{
return 1;
}
//红包最大因子不能超过份数
if ((factor >= portion*100) || (factor < 100))
{
return 2;
}
//printf("left_coin:%d, r_portion:%d, factor:%d, avg_portion:%d, max_portion:%d\n"
// , left_coin, r_portion, factor, avg_portion, max_portion);
for (int i=0; i < portion; i++)
{
int avg_portion = (left_coin+r_portion) / r_portion;
int max_portion = (factor * avg_portion)/100;
if (max_portion == 0) max_portion = 1;
if (left_coin <= 0)
{
printf("left_coin <= 0, left_coin = %d\n", left_coin);
return 3;
}
if (i == portion - 1)
{
this_portion = left_coin;
}
else
{
//至少要留下r_portion-1
int mod_factor = left_coin - r_portion + 1;
if (mod_factor > max_portion)
{
mod_factor = max_portion;
}
int r = rand() % mod_factor;
this_portion = r + 1;//确保this_portion>0
r_portion --;
}
vecnums.push_back(this_portion);
left_coin -= this_portion;
}
return 0;
}
uint32_t gen_rand_seed()
{
struct timeval tv;
gettimeofday(&tv, NULL);
const uint32_t kPrime1 = 61631;
const uint32_t kPrime2 = 64997;
const uint32_t kPrime3 = 111857;
return kPrime1 * static_cast<uint32_t>(getpid())
+ kPrime2 * static_cast<uint32_t>(tv.tv_sec)
+ kPrime3 * static_cast<uint32_t>(tv.tv_usec);
}
void boost_random(vector<int> &vecnums)
{
// create seed
unsigned long seed = gen_rand_seed();
// produces general pseudo random number series with the Mersenne Twister Algorithm
boost::mt19937 rng ( seed );
// uniform distribution on 1...6
boost::uniform_int <> three(0 ,2);
// connects distribution with random series
boost::variate_generator< boost::mt19937&, boost::uniform_int <> > unsix(rng ,three);
for(int i=0;i<3;i++)
{
vecnums.push_back(unsix());
}
}
int test_boost_random(int argc, char* argv[])
{
int test_times = 5000000;
int stat[3][3] = {{0}};
vector<int> vecnums;
for (int i=0;i<test_times;i++)
{
vecnums.clear();
boost_random(vecnums);
for(int i=0;i<3;i++) //i 数字
{
for(int j=0;j<3;j++) // j位置
{
int n=vecnums[j];
stat[n][j] += 1;
}
}
}
for (int i=0;i<3;i++)
{
printf("%d %d %d %d\n", i+1, stat[i][0],stat[i][1],stat[i][2]);
}
return 0;
}
int gen_rfv_redpacket(int portion, int coin, int factor, vector<int> &vecnums)
{
int avg_port = coin / portion;
int var_port = 20;
int rsv_port = avg_port - var_port;
if (rsv_port < 0)
{
rsv_port = 0;
}
int var_coin = coin - rsv_port * portion;
if(0 != gen_redpacket1(portion,var_coin,factor,vecnums))
{
printf("gen_redpacket1 failed! portion:%d, var_coin:%d, factor:%d\n", portion, var_coin, factor);
return 1;
}
for(size_t i = 0; i < vecnums.size(); i++)
{
vecnums[i] += rsv_port;
}
sort(vecnums.begin(), vecnums.end());
//shuffle_vecint(vecnums);
}
int gen_lucky_redpacket(int portion, int coin, int factor, vector<int> &vecnums)
{
//幸运数出现的概率
map<int, int> map_lucky_rate;
map_lucky_rate[2017] = 0;
map_lucky_rate[1314] = 0;
map_lucky_rate[381] = 0;
int retry_times = 3;
int useq = rand();
redo:
vecnums.clear();
retry_times --;
if (retry_times < 0)
{
//DEBUG_P(LOG_ERROR, "CCreateRedPacketMineSweep::GetRateNew. reach retry limit!\n");
printf("CCreateRedPacketMineSweep::GetRateNew. reach retry limit!useq:%d\n", useq);
return false;
}
if(0 != gen_redpacket1(portion,coin,211,vecnums))
{
printf("CCreateRedPacketMineSweep::GetRateNew err, portion = %d, coin = %d, useq:%d\n", portion, coin, useq);
return false;
}
for (unsigned i = 0; i < vecnums.size(); i++)
{
int c = vecnums[i];
if (map_lucky_rate.find(c) != map_lucky_rate.end())