Welcome Guest, Not a member yet? Register   Sign In
Random Numbers Question
#1

[eluser]Mantra of Doom[/eluser]
Hi all,

I'm trying to create a dice roller as a part of my RPG website and my testers are saying that the results aren't random enough. The results favor too many highs and lows and not enough in the middle.

How do you normally deal with trying to get true random numbers? Can anyone suggest a better way?

The die roller allows for multiple dice of any number of sides. Once the roll is made, it posts to a log so that the results are shown to all to cut down cheating.

Controller
Code:
function die_roller() {

        $data = $this->home->general(); //load some shared data from model

        $this->form //Uses the Form Generation Library to populate form.
                ->fieldset('Die Roller');

        if (!$this->ion_auth->logged_in()) {
            $this->form->hidden('player_id', '0', 'required|trim|xxs_clean');
        } else {
            $this->form->hidden('player_id', $data['user']->id, 'required|trim|xxs_clean');
        }
        $this->form->html('<div id="leftside">')
    ->text('character', 'Character Name', 'required|trim|xxs_clean|callback_charname_check')
                ->text('description', 'Action', 'required|trim|xxs_clean')
    ->html('</div><div id="rightside">')
    ->text('sides', '# of Sides', 'required|trim|numeric|xxs_clean')
                ->text('dice', '# of Dice', 'required|trim|numeric|xxs_clean')
    ->html('</div>')
                ->submit('Roll!')
                ->model('home_model', 'roll')
                ->onsuccess('redirect', array('die_roller'), 'refresh'); // When finished, refresh this page for updated results

        $data['form'] = $this->form->get();
        $data['errors'] = $this->form->errors;

        $data['dice'] = $this->home->get_roll();

        $data['websubtitle'] = 'Die Roller';
        $data['content'] = 'about/die_roller';
        $this->load->view('template', $data);
  
    }
Model
Code:
function roll(&$form, $data){
  $sides = $data['sides'];
  $dice = $data['dice'];

  // Run the random numbers until the number of dice has been reached
  $rolls=array();
   for ($i=1; $i<=$dice; $i++){
    $rolls[] = rand(1, $sides); // Put the rolls into an array
   }
  
  // Arrays can't go into the db, so implode it into a string with a comma seperator
  $clean_roll = implode(", ",$rolls);

  $data = array(
     'player_id'=>humanize($data['player_id']), //player id
     'character'=>humanize($data['character']), //character name
     'action'=>$data['description'], //action
     'sides'=>$sides, // # of sides
     'dice'=>$dice, // # of dice
     'result'=>$clean_roll, // results as a string
     );
  $this->db->set('time', 'NOW()', FALSE);
  
  // Inserts roll data into the dice roller table
  $this->db->insert('rolls', $data);




Theme © iAndrew 2016 - Forum software by © MyBB