Statistics for MySQL  0.9
rand_norm.cc
Go to the documentation of this file.
00001 /* rand_norm.cc (rand_norm) */
00002 
00003 /***********************************************************************
00004 *  This code is part of Statistics for MySQL.
00005 *
00006 *  Copyright (C) 2011 Heinrich Schuchardt (xypron.glpk@gmx.de)
00007 *
00008 *  Licensed under the Apache License, Version 2.0 (the "License");
00009 *  you may not use this file except in compliance with the License.
00010 *  You may obtain a copy of the License at
00011 *
00012 *      http://www.apache.org/licenses/LICENSE-2.0
00013 *
00014 *  Unless required by applicable law or agreed to in writing, software
00015 *  distributed under the License is distributed on an "AS IS" BASIS,
00016 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00017 *  See the License for the specific language governing permissions and
00018 *  limitations under the License.
00019 ***********************************************************************/
00020 
00030 #include "sqlstat.h"
00031 #include "sqlrand.h"
00032 
00033 using sqlstat::MersenneTwister;
00034 
00046 my_bool rand_norm_init(UDF_INIT *initid, UDF_ARGS *args, char *message) {
00047   int i;
00048 
00049   if (!sqlstat_plugin_isloaded()) {
00050     strcpy(message,"libsqlstat plugin is not loaded. "
00051       "Use 'INSTALL PLUGIN' for installation.");
00052     return 1;
00053   }
00054   
00055   if (args->arg_count > 2 || args->arg_count < 0) {
00056     strcpy(message,"rand_norm() takes up to two arguments");
00057     return 1;
00058   }
00059 
00060   for ( i = 0; i < (int) args->arg_count; i++) {
00061     args->arg_type[i] = REAL_RESULT;
00062   }
00063 
00064   initid->maybe_null = 0;
00065   initid->decimals   = NOT_FIXED_DEC;
00066   initid->max_length = 13 + initid->decimals;
00067   initid->ptr        = NULL;
00068   initid->const_item = 0;
00069   return 0;
00070 }
00071 
00084 double rand_norm(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error) {
00085   double mu = 0;
00086   double sigma = 1;  
00087 
00088   if (args->arg_count >= 1) {
00089     if (!args->args[0]) {
00090       *error = 1;
00091     } else {
00092     sigma = *((double*) args->args[0]);
00093     }
00094   }
00095   if (args->arg_count >= 2) {
00096     if (!args->args[1]) {
00097       *error = 1;
00098     } else {
00099     mu = *((double*) args->args[1]);
00100     }
00101   }
00102   return mu + sigma * MersenneTwister::gaussian();
00103 }
00104 
00111 void rand_norm_deinit(UDF_INIT *initid) {
00112 }
 All Classes Files Functions Variables Typedefs Defines