History

update to ck_random()

Ge Wang committed 3 years ago

Commit 2f2c9671 parent 170b70a7 files · +74 −75Browse files

Showing the changes to src/core/ulib_std.cpp (1 of 7 files). Show all

Unified · Split

src/core/ulib_std.cpp+18−26
@@ -152,6 +152,8 @@ static t_CKUINT Cereal_offset_data = 0; · 151 unmodified lines
152#endif152#endif
153 153
154 154
155// variable to random max
156static t_CKINT g_randMax = CK_RANDOM_MAX;
155 157
156 158
157//-----------------------------------------------------------------------------159//-----------------------------------------------------------------------------
@@ -187,7 +189,7 @@ DLL_QUERY libstd_query( Chuck_DL_Query * QUERY ) · 29 unmodified lines
187 QUERY->doc_func( QUERY, "Return absolute value of float." );189 QUERY->doc_func( QUERY, "Return absolute value of float." );
188 190
189 // add rand191 // add rand
190 QUERY->add_sfun( QUERY, rand_impl, "int", "rand"); //! return int between 0 and RAND_MAX192 QUERY->add_sfun( QUERY, rand_impl, "int", "rand"); //! return int between 0 and max
191 QUERY->doc_func( QUERY, "generate a random integer between 0 and Std.RAND_MAX. (NOTE: this is deprecated; use Math.random())." );193 QUERY->doc_func( QUERY, "generate a random integer between 0 and Std.RAND_MAX. (NOTE: this is deprecated; use Math.random())." );
192 194
193 // add rand2195 // add rand2
@@ -209,7 +211,11 @@ DLL_QUERY libstd_query( Chuck_DL_Query * QUERY ) · 15 unmodified lines
209 // add srand211 // add srand
210 QUERY->add_sfun( QUERY, srand_impl, "void", "srand" );212 QUERY->add_sfun( QUERY, srand_impl, "void", "srand" );
211 QUERY->add_arg( QUERY, "int", "seed" );213 QUERY->add_arg( QUERY, "int", "seed" );
212 QUERY->doc_func( QUERY, "seed the random number generator. Different seeds will likely generate different sequences of random numbers even if the seeds are close together; alternatively, a sequence of random numbers can be repeated by setting the same seed." );214 QUERY->doc_func( QUERY, "seed the random number generator. Different seeds will likely generate different sequences of random numbers even if the seeds are close together; alternatively, a sequence of random numbers can be repeated by setting the same seed. (NOTE: this is deprecated; use Math.srandom())" );
215
216 // random max
217 QUERY->add_svar( QUERY, "int", "RAND_MAX", TRUE, &g_randMax );
218 QUERY->doc_var( QUERY, "The largest possible value returned by Std.rand()." );
213 219
214 // add sgn220 // add sgn
215 QUERY->add_sfun( QUERY, sgn_impl, "float", "sgn" ); //! return sign of value (-1, 0, 1)221 QUERY->add_sfun( QUERY, sgn_impl, "float", "sgn" ); //! return sign of value (-1, 0, 1)
@@ -328,7 +334,7 @@ DLL_QUERY libstd_query( Chuck_DL_Query * QUERY ) · 112 unmodified lines
328 334
329 // seed rand()335 // seed rand()
330 // (NOTE: ck_srandom(time(NULL)) is invoked in ulib_math.cpp)336 // (NOTE: ck_srandom(time(NULL)) is invoked in ulib_math.cpp)
331 srand( (unsigned)time( NULL ) );337 // srand( (unsigned)time( NULL ) );
332 338
333 Chuck_DL_Func * func = NULL;339 Chuck_DL_Func * func = NULL;
334 340
@@ -623,20 +629,6 @@ error: · 288 unmodified lines
623 return FALSE;629 return FALSE;
624}630}
625 631
626
627//#define RAND_INV_RANGE(r) (RAND_MAX / (r))
628//
629//int irand_exclusive ( int max ) {
630// int x = ::rand();
631//
632// while (x >= max * RAND_INV_RANGE (max))
633// x = ::rand();
634//
635// x /= RAND_INV_RANGE (max);
636// return x;
637//}
638
639
640// abs632// abs
641CK_DLL_SFUN( abs_impl )633CK_DLL_SFUN( abs_impl )
642{634{
@@ -655,20 +647,22 @@ CK_DLL_SFUN( fabs_impl ) · 12 unmodified lines
655// rand647// rand
656CK_DLL_SFUN( rand_impl )648CK_DLL_SFUN( rand_impl )
657{649{
658 RETURN->v_int = ::rand();650 // 1.5.0.1 (ge) use ck_random()
651 RETURN->v_int = ck_random();
659}652}
660 653
661// randf654// randf
662CK_DLL_SFUN( randf_impl )655CK_DLL_SFUN( randf_impl )
663{656{
664 RETURN->v_float = ( 2.0 * ::rand() / (t_CKFLOAT)RAND_MAX - 1.0 );657 // 1.5.0.1 (ge) use ck_random() and CK_RANDOM_MAX
658 RETURN->v_float = ( 2.0 * ck_random() / (t_CKFLOAT)CK_RANDOM_MAX - 1.0 );
665}659}
666 660
667// randf661// randf
668CK_DLL_SFUN( rand2f_impl )662CK_DLL_SFUN( rand2f_impl )
669{663{
670 t_CKFLOAT min = GET_CK_FLOAT(ARGS), max = *((t_CKFLOAT *)ARGS + 1);664 t_CKFLOAT min = GET_CK_FLOAT(ARGS), max = *((t_CKFLOAT *)ARGS + 1);
671 RETURN->v_float = min + (max-min)*(::rand()/(t_CKFLOAT)RAND_MAX);665 RETURN->v_float = min + (max-min)*(::ck_random()/(t_CKFLOAT)CK_RANDOM_MAX);
672}666}
673 667
674// randi668// randi
@@ -681,18 +675,15 @@ CK_DLL_SFUN( rand2_impl ) // inclusive. · 6 unmodified lines
681 {675 {
682 RETURN->v_int = min;676 RETURN->v_int = min;
683 }677 }
684 //else if ( range < RAND_MAX / 2 ) {
685 // RETURN->v_int = ( range > 0 ) ? min + irand_exclusive(1 + range) : max + irand_exclusive ( -range + 1 ) ;
686 //}
687 else678 else
688 {679 {
689 if( range > 0 )680 if( range > 0 )
690 {681 {
691 RETURN->v_int = min + (t_CKINT)( (1.0 + range) * ( ::rand()/(RAND_MAX+1.0) ) );682 RETURN->v_int = min + (t_CKINT)( (1.0 + range) * ( ::ck_random()/(CK_RANDOM_MAX+1.0) ) );
692 }683 }
693 else684 else
694 {685 {
695 RETURN->v_int = min - (t_CKINT)( (-range + 1.0) * ( ::rand()/(RAND_MAX+1.0) ) );686 RETURN->v_int = min - (t_CKINT)( (-range + 1.0) * ( ::ck_random()/(CK_RANDOM_MAX+1.0) ) );
696 }687 }
697 }688 }
698}689}
@@ -701,7 +692,8 @@ CK_DLL_SFUN( rand2_impl ) // inclusive. · 2 unmodified lines
701CK_DLL_SFUN( srand_impl )692CK_DLL_SFUN( srand_impl )
702{693{
703 t_CKINT seed = GET_CK_INT(ARGS);694 t_CKINT seed = GET_CK_INT(ARGS);
704 srand( (unsigned)seed );695 // 1.5.0.1 (ge) updated to ck_srandom()
696 ck_srandom( (unsigned)seed );
705}697}
706 698
707// sgn699// sgn

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t