Dice number generation

Gameplay discussion, questions and ideas.
User avatar
Shevarash
FORGER CODER
Posts: 2944
Joined: Fri Dec 29, 2000 6:01 am

Dice number generation

Postby Shevarash » Sun Jan 21, 2018 8:49 pm

Here's the code for dice rolling. The first block is the C code for the dice command, followed by the Rust code that actually generates the number.

Code: Select all

void
do_dice(P_char ch, char *arg, int cmd)
{
   char Gbuf1[MAX_STRING_LENGTH];
   char Gbuf2[MAX_STRING_LENGTH];
   int rolls, size, result;

   if(!SanityCheck(ch, "do_dice"))
      return;

   if(!*arg) {
      send_to_char("You need to specify the dice size!\n  ex: 'dice 1 8'  - one roll with an eight sided die.\n", ch);
      return;
   }

   half_chop(arg, Gbuf1, Gbuf2);
   if(is_number(Gbuf1)) {
      rolls = atoi(Gbuf1);
      if(rolls < 1 || rolls > 10000) {
         send_to_char("Sorry bub, the first parameter is out of range.\n", ch);
         return;
      }
   }
   else {
      send_to_char("The first parameter needs to be a number. Try again.\n", ch);
      return;
   }

   if(!*Gbuf2) {
      send_to_char("You need to specify the dice size as the second parameter.\n", ch);
      return;
   }

   one_argument(Gbuf2, Gbuf1);
   if(is_number(Gbuf1)) {
      size = atoi(Gbuf1);
      if(size < 1 || size > 10000) {
         send_to_char("Sorry bub, the second number is out of range.\n", ch);
         return;
      }
   }
   else {
      send_to_char("The second parameter needs to be a number. Try again.\n", ch);
      return;
   }

  result = venom_roll_dice(rolls, size);
//   result = dice(rolls, size);

   sprintf(Gbuf1, "You roll a %d sided dice %d times, the total result is: &+B%d&N\n", size, rolls, result);
   send_to_char(Gbuf1, ch);

   sprintf(Gbuf1, "A %d sided dice is rolled by %s %d times, the total result is: &+B%d&N\n", size, GET_NAME(ch), rolls, result);
   send_to_room(Gbuf1, ch->in_room);

   return;
}



Code: Select all

/// Dice: dice rolling and random number generation

extern crate rand;
use self::rand::distributions::{IndependentSample, Range};

/// Roll some dice!

#[no_mangle]
pub fn venom_roll_dice(size: i32, num: i32) -> i32 {
    let between = Range::new(1, num + 1);
    let mut rng = rand::thread_rng();
    let mut result = 0;

    for _ in 0..size {
        result += between.ind_sample(&mut rng);
    }

    result
}

/// Get a random i32 between a range.

#[no_mangle]
pub fn venom_get_random_i32(min: i32, max: i32) -> i32 {
    let between = Range::new(min, max + 1);
    let mut rng = rand::thread_rng();

    between.ind_sample(&mut rng)
}
Shevarash -- Code Forger of TorilMUD
Marthammor
Staff Member - Areas
Posts: 834
Joined: Tue Jul 15, 2003 9:00 am
Location: On a Rocky Tor Overlooking a Storm-Ridden Landscape
Contact:

Re: Dice number generation

Postby Marthammor » Mon Jan 22, 2018 4:32 am

Is that the old code? I don't see the newer stuff in there to handle gerin's rolls.
Afu
Sojourner
Posts: 82
Joined: Sun Oct 01, 2017 3:58 pm

Re: Dice number generation

Postby Afu » Sat Jan 26, 2019 3:43 am

source

Code: Select all

Function rand::thread_rng [−] [src]
pub fn thread_rng() -> ThreadRng
[−]
Retrieve the lazily-initialized thread-local random number generator, seeded by the system. Intended to be used in method chaining style, e.g. thread_rng().gen::<i32>().

After generating a certain amount of randomness, the RNG will reseed itself from the operating system or, if the operating system RNG returns an error, a seed based on the current system time.

The internal RNG used is platform and architecture dependent, even if the operating system random number generator is rigged to give the same sequence always. If absolute consistency is required, explicitly select an RNG, e.g. IsaacRng or Isaac64Rng.


based on the minute differences in time, so is waiting to roll better or doing it first???
need to run some tests.

Return to “GCC: Gameplay Discussion”

Who is online

Users browsing this forum: No registered users and 12 guests