~drizzle-trunk/drizzle/development

« back to all changes in this revision

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

  • Committer: Brian Aker
  • Date: 2009-01-24 09:43:35 UTC
  • Revision ID: brian@gir-3.local-20090124094335-6qdtvc35gl5fvivz
Adding in an example singe thread scheduler

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
18
 */
19
19
 
20
 
#include "config.h"
 
20
#include <drizzled/server_includes.h>
 
21
#include CSTDINT_H
21
22
#include <drizzled/function/math/rand.h>
 
23
#include <libdrizzle/password.h>
22
24
#include <drizzled/session.h>
23
25
 
24
 
namespace drizzled
25
 
{
26
 
 
27
 
static uint32_t sql_rnd()
28
 
{
29
 
  return (uint32_t) (rand() * 0xffffffff); /* make all bits random */
30
 
}
31
 
 
32
 
 
33
26
void Item_func_rand::seed_random(Item *arg)
34
27
{
35
28
  /*
36
29
    TODO: do not do reinit 'rand' for every execute of PS/SP if
37
30
    args[0] is a constant.
38
31
  */
39
 
  uint64_t tmp= (uint64_t) arg->val_int();
40
 
  _seed_random_int(tmp * 0x10001L + 55555555L, tmp * 0x10000001L);
41
 
}
42
 
 
43
 
void Item_func_rand::_seed_random_int(uint64_t new_seed1, uint64_t new_seed2)
44
 
{
45
 
  max_value= 0x3FFFFFFFL;
46
 
  max_value_dbl=(double) max_value;
47
 
  seed1= new_seed1 % max_value;
48
 
  seed2= new_seed2 % max_value;
 
32
  uint32_t tmp= (uint32_t) arg->val_int();
 
33
  randominit(rand, (uint32_t) (tmp*0x10001L+55555555L),
 
34
             (uint32_t) (tmp*0x10000001L));
49
35
}
50
36
 
51
37
bool Item_func_rand::fix_fields(Session *session,Item **ref)
52
38
{
53
39
  if (Item_real_func::fix_fields(session, ref))
54
40
    return true;
55
 
 
56
41
  used_tables_cache|= RAND_TABLE_BIT;
57
42
  if (arg_count)
58
43
  {                                     // Only use argument once in query
60
45
      No need to send a Rand log event if seed was given eg: RAND(seed),
61
46
      as it will be replicated in the query as such.
62
47
    */
 
48
    if (!rand && !(rand= (struct rand_struct*)
 
49
                   session->alloc(sizeof(*rand))))
 
50
      return true;
 
51
 
63
52
    if (args[0]->const_item())
64
 
      seed_random(args[0]);
 
53
      seed_random (args[0]);
65
54
  }
66
55
  else
67
56
  {
68
 
    uint64_t tmp= sql_rnd();
69
 
    _seed_random_int(tmp + (uint64_t) ref, tmp + (uint64_t) session->thread_id);
 
57
    rand= &session->rand;
70
58
  }
71
 
 
72
59
  return false;
73
60
}
74
61
 
78
65
  used_tables_cache|= RAND_TABLE_BIT;
79
66
}
80
67
 
 
68
 
81
69
double Item_func_rand::val_real()
82
70
{
83
71
  assert(fixed == 1);
84
72
  if (arg_count && !args[0]->const_item())
85
 
    seed_random(args[0]);
86
 
 
87
 
  seed1= (seed1 * 3 + seed2) % max_value;
88
 
  seed2= (seed1 + seed2 + 33) % max_value;
89
 
  return (((double) seed1) / max_value_dbl);
 
73
    seed_random (args[0]);
 
74
  return my_rnd(rand);
90
75
}
91
76
 
92
 
} /* namespace drizzled */