Statistics for MySQL  1.1
 All Classes Files Functions Variables Typedefs Macros Pages
rand_norm.cc
Go to the documentation of this file.
1 /* rand_norm.cc (rand_norm) */
2 
3 /***********************************************************************
4 * This code is part of Statistics for MySQL.
5 *
6 * Copyright (C) 2011 Heinrich Schuchardt (xypron.glpk@gmx.de)
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 ***********************************************************************/
20 
30 #include "sqlstat.h"
31 #include "sqlrand.h"
32 
34 
46 my_bool rand_norm_init(UDF_INIT *initid, UDF_ARGS *args, char *message) {
47  int i;
48 
49  if (!sqlstat_plugin_isloaded()) {
50  strcpy(message,"libsqlstat plugin is not loaded. "
51  "Use 'INSTALL PLUGIN' for installation.");
52  return 1;
53  }
54 
55  if (args->arg_count > 2 || args->arg_count < 0) {
56  strcpy(message,"rand_norm() takes up to two arguments");
57  return 1;
58  }
59 
60  for ( i = 0; i < (int) args->arg_count; i++) {
61  args->arg_type[i] = REAL_RESULT;
62  }
63 
64  initid->maybe_null = 0;
65  initid->decimals = NOT_FIXED_DEC;
66  initid->max_length = 13 + initid->decimals;
67  initid->ptr = NULL;
68  initid->const_item = 0;
69  return 0;
70 }
71 
84 double rand_norm(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error) {
85  double mu = 0;
86  double sigma = 1;
87 
88  if (args->arg_count >= 1) {
89  if (!args->args[0]) {
90  *error = 1;
91  } else {
92  sigma = *((double*) args->args[0]);
93  }
94  }
95  if (args->arg_count >= 2) {
96  if (!args->args[1]) {
97  *error = 1;
98  } else {
99  mu = *((double*) args->args[1]);
100  }
101  }
102  return mu + sigma * MersenneTwister::gaussian();
103 }
104 
111 void rand_norm_deinit(UDF_INIT *initid) {
112 }
my_bool rand_norm_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
Called before first usage of function.
Definition: rand_norm.cc:46
Definition of functions for UDFs and plugins.
Definitions for random number generations.
double rand_norm(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error)
Retrieve a variate of the normal distribution.
Definition: rand_norm.cc:84
#define NOT_FIXED_DEC
Maximum number of digits in double As defined in mysql/sql_string.h.
Definition: sqlstat.h:77
static double gaussian()
Returns variate of the normal distribution.
Definition: sqlrand.cc:111
void rand_norm_deinit(UDF_INIT *initid)
Called after last access to function.
Definition: rand_norm.cc:111
Mersenne Twister random number generator.
Definition: sqlrand.h:60
int sqlstat_plugin_isloaded()
Check if plugin loaded.
Definition: sqlplugin.cc:70