~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/function/math/rand.cc

Moved the last of the libdrizzleclient calls into Protocol.

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
#include <drizzled/server_includes.h>
21
21
#include CSTDINT_H
22
22
#include <drizzled/function/math/rand.h>
23
 
#include <libdrizzleclient/password.h>
24
23
#include <drizzled/session.h>
25
24
 
26
25
void Item_func_rand::seed_random(Item *arg)
30
29
    args[0] is a constant.
31
30
  */
32
31
  uint64_t tmp= (uint64_t) arg->val_int();
33
 
  drizzleclient_randominit(rand, (tmp*0x10001L+55555555L),
34
 
                           (tmp*0x10000001L));
 
32
  _seed_random_int(tmp * 0x10001L + 55555555L, tmp * 0x10000001L);
 
33
}
 
34
 
 
35
void Item_func_rand::_seed_random_int(uint64_t new_seed1, uint64_t new_seed2)
 
36
{
 
37
  max_value= 0x3FFFFFFFL;
 
38
  max_value_dbl=(double) max_value;
 
39
  seed1= new_seed1 % max_value;
 
40
  seed2= new_seed2 % max_value;
35
41
}
36
42
 
37
43
bool Item_func_rand::fix_fields(Session *session,Item **ref)
38
44
{
39
45
  if (Item_real_func::fix_fields(session, ref))
40
46
    return true;
 
47
 
41
48
  used_tables_cache|= RAND_TABLE_BIT;
42
49
  if (arg_count)
43
50
  {                                     // Only use argument once in query
45
52
      No need to send a Rand log event if seed was given eg: RAND(seed),
46
53
      as it will be replicated in the query as such.
47
54
    */
48
 
    if (!rand && !(rand= (struct rand_struct*)
49
 
                   session->alloc(sizeof(*rand))))
50
 
      return true;
51
 
 
52
55
    if (args[0]->const_item())
53
 
      seed_random (args[0]);
 
56
      seed_random(args[0]);
54
57
  }
55
58
  else
56
59
  {
57
 
    rand= &session->rand;
 
60
    uint64_t tmp= sql_rnd();
 
61
    _seed_random_int(tmp + (uint64_t) ref, tmp + (uint64_t) session->thread_id);
58
62
  }
 
63
 
59
64
  return false;
60
65
}
61
66
 
65
70
  used_tables_cache|= RAND_TABLE_BIT;
66
71
}
67
72
 
68
 
 
69
73
double Item_func_rand::val_real()
70
74
{
71
75
  assert(fixed == 1);
72
76
  if (arg_count && !args[0]->const_item())
73
 
    seed_random (args[0]);
74
 
  return drizzleclient_my_rnd(rand);
 
77
    seed_random(args[0]);
 
78
 
 
79
  seed1= (seed1 * 3 + seed2) % max_value;
 
80
  seed2= (seed1 + seed2 + 33) % max_value;
 
81
  return (((double) seed1) / max_value_dbl);
75
82
}
76