22#include <drizzled/plugin/function.h>
23#include <drizzled/item/func.h>
24#include <drizzled/function/math/real.h>
25#include <drizzled/session.h>
30class RandFunction :
public Item_real_func
36 void _seed_random_int(uint64_t new_seed1, uint64_t new_seed2);
39 RandFunction() :Item_real_func() {}
41 const char *func_name()
const {
return "rand"; }
43 void update_used_tables();
48 return (n == 0 || n == 1);
52 void seed_random (
Item * val);
55static uint32_t sql_rnd()
57 return (uint32_t) (rand() * 0xffffffff);
61void RandFunction::seed_random(
Item *arg)
67 uint64_t tmp= (uint64_t) arg->
val_int();
68 _seed_random_int(tmp * 0x10001L + 55555555L, tmp * 0x10000001L);
71void RandFunction::_seed_random_int(uint64_t new_seed1, uint64_t new_seed2)
73 max_value= 0x3FFFFFFFL;
74 max_value_dbl=(double) max_value;
75 seed1= new_seed1 % max_value;
76 seed2= new_seed2 % max_value;
79bool RandFunction::fix_fields(
Session *session,
Item **ref)
81 if (Item_real_func::fix_fields(session, ref))
84 used_tables_cache|= RAND_TABLE_BIT;
96 uint64_t tmp= sql_rnd();
97 _seed_random_int(tmp + (uint64_t) ref, tmp + (uint64_t) session->thread_id);
103void RandFunction::update_used_tables()
105 Item_real_func::update_used_tables();
106 used_tables_cache|= RAND_TABLE_BIT;
113 seed_random(args[0]);
115 seed1= (seed1 * 3 + seed2) % max_value;
116 seed2= (seed1 + seed2 + 33) % max_value;
117 return (((
double) seed1) / max_value_dbl);
125 registry.add(rand_function);
129DRIZZLE_DECLARE_PLUGIN
141DRIZZLE_DECLARE_PLUGIN_END;
bool check_argument_count(int n)
virtual int64_t val_int()=0
TODO: Rename this file - func.h is stupid.