Show
Ignore:
Timestamp:
08/05/07 00:26:15 (16 months ago)
Author:
krobillard
Message:

random handles coord!

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • branches/thune/thread_safe/math.c

    r394 r441  
    653653UR_CALL( uc_random ) 
    654654{ 
    655     if( ur_is(tos, UT_DECIMAL) ) 
    656     { 
    657         ur_decimal(tos) *= genrand_real2(); 
    658     } 
    659     else if( ur_is(tos, UT_INT) ) 
    660     { 
    661         if( ur_int(tos) > 0 ) 
    662             ur_int(tos) = (genrand_int32() % ur_int(tos)) + 1; 
    663     } 
    664     else if( ur_isASeries( tos ) ) 
     655    if( ur_isASeries( tos ) ) 
    665656    { 
    666657        int len = ur_seriesLen( tos ); 
    667         if( len < 0 ) 
    668             goto bad_type; 
    669658        if( len > 0 ) 
    670659            tos->series.it += genrand_int32() % (len - tos->series.it); 
    671     } 
    672     else if( ur_is(tos, UT_LOGIC) ) 
    673     { 
    674         ur_logic(tos) = genrand_int32() & 1; 
    675     } 
     660        return; 
     661    } 
     662 
     663    switch( ur_type(tos) ) 
     664    { 
     665        case UT_DECIMAL: 
     666            ur_decimal(tos) *= genrand_real2(); 
     667            break; 
     668 
     669        case UT_INT: 
     670            if( ur_int(tos) > 0 ) 
     671                ur_int(tos) = (genrand_int32() % ur_int(tos)) + 1; 
     672            break; 
     673 
     674        case UT_LOGIC: 
     675            ur_logic(tos) = genrand_int32() & 1; 
     676            break; 
     677 
    676678#ifdef UR_CONFIG_MATH3D 
    677     else if( ur_is(tos, UT_VEC3) ) 
    678     { 
    679         tos->vec3.xyz[0] *= genrand_real2(); 
    680         tos->vec3.xyz[1] *= genrand_real2(); 
    681         tos->vec3.xyz[2] *= genrand_real2(); 
    682     } 
     679        case UT_VEC3: 
     680            tos->vec3.xyz[0] *= genrand_real2(); 
     681            tos->vec3.xyz[1] *= genrand_real2(); 
     682            tos->vec3.xyz[2] *= genrand_real2(); 
     683            break; 
    683684#endif 
    684     else if( ur_isAWord(tos) ) 
    685     { 
    686         unsigned long seed; 
    687  
    688         tos = ur_s_prev(tos); 
    689  
    690         if( ur_is(tos, UT_INT) && ur_int(tos) ) 
    691             seed = ur_int(tos); 
    692         else 
    693             seed = _clockSeed(); 
    694         init_genrand( seed ); 
    695  
    696         UR_S_DROPN( 2 ); 
    697     } 
    698     else 
    699     { 
    700 bad_type: 
    701         ur_throwErr( UR_ERR_DATATYPE, "random does not handle %s", 
    702                      ur_typeName( ur_type(tos) ) ); 
     685        case UT_COORD: 
     686        { 
     687            int i, n; 
     688            for( i = 0; i < tos->coord.len; ++i ) 
     689            { 
     690                n = tos->coord.elem[i]; 
     691                if( n > 0 ) 
     692                    tos->coord.elem[i] = (genrand_int32() % n) + 1; 
     693            } 
     694        } 
     695            break; 
     696 
     697        case UT_WORD: 
     698        case UT_LITWORD: 
     699        { 
     700            unsigned long seed; 
     701 
     702            tos = ur_s_prev(tos); 
     703 
     704            if( ur_is(tos, UT_INT) && ur_int(tos) ) 
     705                seed = ur_int(tos); 
     706            else 
     707                seed = _clockSeed(); 
     708            init_genrand( seed ); 
     709 
     710            UR_S_DROPN( 2 ); 
     711        } 
     712            break; 
     713 
     714        default: 
     715            ur_throwErr( UR_ERR_DATATYPE, "random does not handle %s", 
     716                         ur_typeName( ur_type(tos) ) ); 
     717            break; 
    703718    } 
    704719}