22 #include <drizzled/plugin/function.h>
23 #include <drizzled/item/func.h>
24 #include <drizzled/function/math/real.h>
25 #include <drizzled/session.h>
28 using namespace drizzled;
36 void _seed_random_int(uint64_t new_seed1, uint64_t new_seed2);
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);
55 static uint32_t sql_rnd()
57 return (uint32_t) (rand() * 0xffffffff);
61 void RandFunction::seed_random(
Item *arg)
67 uint64_t tmp= (uint64_t) arg->
val_int();
68 _seed_random_int(tmp * 0x10001L + 55555555L, tmp * 0x10000001L);
71 void 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;
79 bool RandFunction::fix_fields(
Session *session,
Item **ref)
81 if (Item_real_func::fix_fields(session, ref))
84 used_tables_cache|= RAND_TABLE_BIT;
91 if (args[0]->const_item())
96 uint64_t tmp= sql_rnd();
97 _seed_random_int(tmp + (uint64_t) ref, tmp + (uint64_t) session->thread_id);
103 void RandFunction::update_used_tables()
105 Item_real_func::update_used_tables();
106 used_tables_cache|= RAND_TABLE_BIT;
112 if (arg_count && !args[0]->const_item())
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);
120 plugin::Create_function<RandFunction> *rand_function= NULL;
124 rand_function=
new plugin::Create_function<RandFunction>(
"rand");
125 registry.add(rand_function);
129 DRIZZLE_DECLARE_PLUGIN
141 DRIZZLE_DECLARE_PLUGIN_END;