1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2008 Sun Microsystems
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; version 2 of the License.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20
#include <drizzled/server_includes.h>
22
#include <drizzled/function/math/rand.h>
23
#include <drizzled/session.h>
25
void Item_func_rand::seed_random(Item *arg)
28
TODO: do not do reinit 'rand' for every execute of PS/SP if
29
args[0] is a constant.
31
uint64_t tmp= (uint64_t) arg->val_int();
32
_seed_random_int(tmp * 0x10001L + 55555555L, tmp * 0x10000001L);
35
void Item_func_rand::_seed_random_int(uint64_t new_seed1, uint64_t new_seed2)
37
max_value= 0x3FFFFFFFL;
38
max_value_dbl=(double) max_value;
39
seed1= new_seed1 % max_value;
40
seed2= new_seed2 % max_value;
43
bool Item_func_rand::fix_fields(Session *session,Item **ref)
45
if (Item_real_func::fix_fields(session, ref))
48
used_tables_cache|= RAND_TABLE_BIT;
50
{ // Only use argument once in query
52
No need to send a Rand log event if seed was given eg: RAND(seed),
53
as it will be replicated in the query as such.
55
if (args[0]->const_item())
60
uint64_t tmp= sql_rnd();
61
_seed_random_int(tmp + (uint64_t) ref, tmp + (uint64_t) session->thread_id);
67
void Item_func_rand::update_used_tables()
69
Item_real_func::update_used_tables();
70
used_tables_cache|= RAND_TABLE_BIT;
73
double Item_func_rand::val_real()
76
if (arg_count && !args[0]->const_item())
79
seed1= (seed1 * 3 + seed2) % max_value;
80
seed2= (seed1 + seed2 + 33) % max_value;
81
return (((double) seed1) / max_value_dbl);