RseedCmd.h 1.63 KB
Newer Older
Francois Gygi committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
////////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2011 The Regents of the University of California
//
// This file is part of Qbox
//
// Qbox is distributed under the terms of the GNU General Public License
// as published by the Free Software Foundation, either version 2 of
// the License, or (at your option) any later version.
// See the file COPYING in the root directory of this distribution
// or <http://www.gnu.org/licenses/>.
//
////////////////////////////////////////////////////////////////////////////////
//
// RseedCmd.h:
//
////////////////////////////////////////////////////////////////////////////////

#ifndef RSEEDCMD_H
#define RSEEDCMD_H

#include <iostream>
#include <stdlib.h>
#include <iomanip>
#include <time.h>

#include "UserInterface.h"
#include "Sample.h"

class RseedCmd : public Cmd
{
  public:

  Sample* s;

  RseedCmd(Sample *sample) : s(sample) {};

38 39
  const char *name(void) const { return "rseed"; }
  const char *help_msg(void) const
Francois Gygi committed
40 41 42 43
  {
    return
    "\n rseed\n\n"
    " syntax: rseed [seed_value]\n\n"
44
    "   The rseed command initializes the random number generator.\n"
Francois Gygi committed
45 46 47 48 49 50
    "   If no argument is given, the time() function is used as a seed value"
    "\n\n";
  }

  int action(int argc, char **argv)
  {
51
    int seed = (int) time(0);
Francois Gygi committed
52
    if ( argc == 2 )
53 54 55 56 57 58 59 60 61 62 63 64 65 66
      seed = atoi(argv[1]);
    else
    {
      if ( ui->onpe0() )
      {
        s->ctxt_.ibcast_send(1,1,&seed,1);
        cout << "<seed> " << seed << " </seed>" << endl;
      }
      else
      {
        s->ctxt_.ibcast_recv(1,1,&seed,1,0,0);
      }
    }
    srand48((long int)seed);
Francois Gygi committed
67 68 69 70
    return 0;
  }
};
#endif