Akasen's Profile User Rating: -----

Reputation: 1 Apprentice
Group:
Members
Active Posts:
47 (0.05 per day)
Joined:
13-August 12
Profile Views:
833
Last Active:
User is offline May 20 2015 08:15 PM
Currently:
Offline

Previous Fields

Country:
US
OS Preference:
Windows
Favorite Browser:
FireFox
Favorite Processor:
Who Cares
Favorite Gaming Platform:
PC
Your Car:
Who Cares
Dream Kudos:
0

Latest Visitors

Icon   Akasen has not set their status

Posts I've Made

  1. In Topic: [C] Dice Rolling program working in Debug but not Release Code::Blocks

    Posted 26 Jan 2015

    So I am currently moving towards the next step of all this, which was to hook this all into a GUI library experiment with that after getting the logic sorted. I seem to have an issue, probably because I didn't plan this all through, of not knowing if C is really a good choice for this endeavor.

    I'm sorta just wondering how I should be going about that at all, or am I still too early in working on this to plan such an endeavor?
  2. In Topic: [C] Dice Rolling program working in Debug but not Release Code::Blocks

    Posted 24 Jan 2015

    I guess there's a lot I got to learn overall in the scheme of programming and such if I keep making these haphazard mistakes.

    So I guess starting with that snippet of code I probably butchered, I didn't think the logic was originally meant to go outside the loop. Or rather, I didn't even think that logic such as what I put in was meant to go anywhere. I was just analyzing it and thought that some if statements would be a good idea. It struck me and I experimented and it worked. Mostly.

    As for the variable "small", I just don't know. I don't think out of the box much, so when I initially saw it I just thought "Well okay, this function takes in a variable called 'small' which is the pointer to some other variable. I think." It didn't really occur to me that just initializing "small" inside the function. Or I just didn't know what it meant ultimately. I assume within the confines within my own code, the number produced by "diceRoll()" is being stored in "smallNumber" back in the function of "operation()". But then that could be totally wrong anyways.

    Hindsight, most of what I am doing is a cross between bashing rocks and throwing darts at a wall.

    So if you don't mind me picking apart and trying to understand your code that you gave that redefines statCalc, am I right to assume

    1. Passes in the variable dice from prior input
    2. Initializes the variables "i", "total", and "small" as integers
    3. Assigns a roll for total and small, which should both have the same number assigned.
    4. Enters a for loop where until "i" is greater than whatever number is dice.
    5. Initializes integer variable "roll" which is equal to the return value of diceRoll()
    6. The number rolled is then added to "total" (total += roll)
    7. An if statement is checking to see if the number just rolled is less than the value that is in small and then small is set to that value
    (Loop continues while i<dice)
    8. If statement for if only two dice were present where in which six is added to the total
    9. Else If statement for if indeed four dice were ever present, then the lowest dice ever rolled is subtracted from the total amount, making it as though a dice were removed.
    10 return of total.

    I believe I'm understanding this correctly. I don't want to be misconstruing anything.

    Then I have some questions regarding the code you posted. Why "define" and why "typedef"? I believe I have seen these before, but I've never understood why, particularly "typedef", they would be used. Wouldn't it be just as good to use the types that C has and to define variables within your code rather than defining them that way?
  3. In Topic: [C] Dice Rolling program working in Debug but not Release Code::Blocks

    Posted 23 Jan 2015

    I'm back once more to proudly say that not only was I able to get the code working, I was able to actually finish everything :D/>

    I revised part of my code and added one more function, but it all works!

    This is the code as I have it now. Would love to hear what you all think :D/>

    I am also including a huge amount of commented out thought that I wrote at the bottom.

    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <string.h>
    
    #ifdef SPECIAL_RANDOMIZER
    #include "tinymt32.c"
    #endif // SPECIAL_RANDOMIZER
    
    int statCalc(int dice, int *small);
    int diceRoll();
    int operation(int dice);
    
    int main()
    {
        printf("Welcome to the Stat Roller!\n");
        printf("This Stat Roller can use the Standard, Classic, or Heroic rolls to determine stats\n");
    
        int choice;
        int dice;
    
        #ifndef SPECIAL_RANDOMIZER
        srand(time(NULL));
        #endif // SPECIAL_RANDOMIZER
    
        do
        {
            printf("\n1. Standard\n2. Classic\n3. Heroic\nChoose a roll type: ");
            choice = 9;
            scanf("%i", &choice);
    
                if(choice == 1)
                {
                    int dice = 4;
                    operation(dice);
                }
                if(choice == 2)
                {
                    int dice = 3;
                    operation(dice);
                }
                if(choice == 3)
                {
                    int dice = 2;
                    operation(dice);
                }
        }while(choice != 0);
        return 0;
    }
    
    int operation(int dice)
    {
        int stats[6];
        char player_continue[4];
    
        int smallNumber = 3;
        int *small = &smallNumber;
    
        do
            {
                // Rolls for the six player stats using stat_roll function
                int x;
                for(x = 0; x <= 5; x++)
                {
                    stats[x] = statCalc(dice, small);
                }
    
                // Prints all stats on one line. Create new blank line
                printf("\n%i, %i, %i, %i, %i, %i\n",
                        stats[0], stats[1], stats[2], stats[3], stats[4], stats[5]);
    
                // Asks user if they wish to continue
                // At the moment, anything will allow the program to roll again
                printf("Enter \'n\' to exit. Enter \'y\' to continue: ");
                scanf("%s", player_continue);
            }while(strcmp(player_continue, "n"));
    }
    
    #ifdef SPECIAL_RANDOMIZER
    int diceRoll()
    {
        static tinymt32_t tinymt;
        return tinymt32_generate_uint32(&tinymt)%6+1;
    }
    #else
    int diceRoll()
    {
        return rand()%6+1;
    }
    #endif // SPECIAL_RANDOMIZER
    
    int statCalc(int dice, int *small)
    {
        //printf("Dice: %i \n\n", dice);
        // Initialization of variables
        // i and total
        int i, total;
    
        // total is equal to the pointer of small which is equal
        // to the return value of diceRoll(). I think
        // Why is a pointer used?
        total = *small = diceRoll();
        //For loop
        for(i = 1; i < dice; i++) {
            int roll = diceRoll();
            total += roll;
    
            if (dice > 3) { if (roll<*small) { total -= roll; } }
        }
        if (dice == 2) { total += 6; }
        return total;
    }
    
    
    /*
    
    Particular issue has been realized.
    I wish to broaden the scope of this so that Pathfinder/D&D's other systems
    for rolling stats is taken into account as well. Future steps would be to
    add a GUI interface on top of all this. What may need to be done is to have the
    function 'stat_roll' accept an X amount for the variable size for rolls and then to have
    the search functions also call upon this size.
    
    To elaborate, Pathfinder details at least three basic roll systems for Ability Score generation.
    
    Standard makes you roll 4d6 and discard the lowest, adding the rest of the numbers.
    
    Classic makes you roll only 3d6 and just add them there and then.
    
    Heroic makes you roll 2d6, add the dice, then add 6.
    
    So what I would want to do very much so is have 'stat_roll' accept two variables.
    A variable for dice and a variable signifying if the roll is for a heroic roll.
    Or maybe I could have an if statement that checks the variable for dice amount
    and signifies from there whether a flat six needs to be added to the roll.
    
    */
    
    
  4. In Topic: [C] Dice Rolling program working in Debug but not Release Code::Blocks

    Posted 19 Jan 2015

    I had a bunch of questions typed out, but it is all lost, so I am going to try to remember the important ones.

    So first off pointers, why use them anyways? In the context of my code and some time using Python, I don't really grasp why pointing to the address of a variable is so important.

    Next is on some of Baavgai's code. So "Ifdef" is what are referred to as Macros. My understanding in the scope of how it is all setup in your case of writing is that it all ties to the function "rollDie". Under a particular circumstance I don't understand, it would do the version of the function which has "TinyMT's" generator. If the circumstance isn't met, it just does C's default rand()

    And then the other line of code perplexes me greatly. I sorta get why it works, but I get caught up on why a point is used for "small" and what "small" should even be. I'd commented the code with my own interpretation of it, but stop before the For loop because by then I sorta understand the for loop, just not the pointer being used and what it's even calling.
    int statRoll(int dice, int *small)
    {
        // Initialization of variables
        // i and total
        int i, total;
    
        // total is equal to the pointer of small which is equal
        // to the return value of rollDie(). I think
        // Why is a pointer used?
        total = *small = rollDie();
        //For loop
        for(i = 1; i < dice; i++) {
            int roll = rollDie();
            total += roll;
            if (roll<*small) { *small = roll; }
        }
        return total;
    }
    
  5. In Topic: [C] Dice Rolling program working in Debug but not Release Code::Blocks

    Posted 10 Jan 2015

    Ah, right. I forgot the source code for those.

    #ifndef TINYMT32_H
    #define TINYMT32_H
    /**
     * @file tinymt32.h
     *
     * @brief Tiny Mersenne Twister only 127 bit internal state
     *
     * @author Mutsuo Saito (Hiroshima University)
     * @author Makoto Matsumoto (University of Tokyo)
     *
     * Copyright (C) 2011 Mutsuo Saito, Makoto Matsumoto,
     * Hiroshima University and The University of Tokyo.
     * All rights reserved.
     *
     * The 3-clause BSD License is applied to this software, see
     * LICENSE.txt
     */
    
    #include <stdint.h>
    #include <inttypes.h>
    
    #define TINYMT32_MEXP 127
    #define TINYMT32_SH0 1
    #define TINYMT32_SH1 10
    #define TINYMT32_SH8 8
    #define TINYMT32_MASK UINT32_C(0x7fffffff)
    #define TINYMT32_MUL (1.0f / 4294967296.0f)
    
    #if defined(__cplusplus)
    extern "C" {
    #endif
    
    /**
     * tinymt32 internal state vector and parameters
     */
    struct TINYMT32_T {
        uint32_t status[4];
        uint32_t mat1;
        uint32_t mat2;
        uint32_t tmat;
    };
    
    typedef struct TINYMT32_T tinymt32_t;
    
    void tinymt32_init(tinymt32_t * random, uint32_t seed);
    void tinymt32_init_by_array(tinymt32_t * random, uint32_t init_key[],
    			    int key_length);
    
    #if defined(__GNUC__)
    /**
     * This function always returns 127
     * @param random not used
     * @return always 127
     */
    inline static int tinymt32_get_mexp(
        tinymt32_t * random  __attribute__((unused))) {
        return TINYMT32_MEXP;
    }
    #else
    inline static int tinymt32_get_mexp(tinymt32_t * random) {
        return TINYMT32_MEXP;
    }
    #endif
    
    /**
     * This function changes internal state of tinymt32.
     * Users should not call this function directly.
     * @param random tinymt internal status
     */
    inline static void tinymt32_next_state(tinymt32_t * random) {
        uint32_t x;
        uint32_t y;
    
        y = random->status[3];
        x = (random->status[0] & TINYMT32_MASK)
    	^ random->status[1]
    	^ random->status[2];
        x ^= (x << TINYMT32_SH0);
        y ^= (y >> TINYMT32_SH0) ^ x;
        random->status[0] = random->status[1];
        random->status[1] = random->status[2];
        random->status[2] = x ^ (y << TINYMT32_SH1);
        random->status[3] = y;
        random->status[1] ^= -((int32_t)(y & 1)) & random->mat1;
        random->status[2] ^= -((int32_t)(y & 1)) & random->mat2;
    }
    
    /**
     * This function outputs 32-bit unsigned integer from internal state.
     * Users should not call this function directly.
     * @param random tinymt internal status
     * @return 32-bit unsigned pseudorandom number
     */
    inline static uint32_t tinymt32_temper(tinymt32_t * random) {
        uint32_t t0, t1;
        t0 = random->status[3];
    #if defined(LINEARITY_CHECK)
        t1 = random->status[0]
    	^ (random->status[2] >> TINYMT32_SH8);
    #else
        t1 = random->status[0]
    	+ (random->status[2] >> TINYMT32_SH8);
    #endif
        t0 ^= t1;
        t0 ^= -((int32_t)(t1 & 1)) & random->tmat;
        return t0;
    }
    
    /**
     * This function outputs floating point number from internal state.
     * Users should not call this function directly.
     * @param random tinymt internal status
     * @return floating point number r (1.0 <= r < 2.0)
     */
    inline static float tinymt32_temper_conv(tinymt32_t * random) {
        uint32_t t0, t1;
        union {
    	uint32_t u;
    	float f;
        } conv;
    
        t0 = random->status[3];
    #if defined(LINEARITY_CHECK)
        t1 = random->status[0]
    	^ (random->status[2] >> TINYMT32_SH8);
    #else
        t1 = random->status[0]
    	+ (random->status[2] >> TINYMT32_SH8);
    #endif
        t0 ^= t1;
        conv.u = ((t0 ^ (-((int32_t)(t1 & 1)) & random->tmat)) >> 9)
    	      | UINT32_C(0x3f800000);
        return conv.f;
    }
    
    /**
     * This function outputs floating point number from internal state.
     * Users should not call this function directly.
     * @param random tinymt internal status
     * @return floating point number r (1.0 < r < 2.0)
     */
    inline static float tinymt32_temper_conv_open(tinymt32_t * random) {
        uint32_t t0, t1;
        union {
    	uint32_t u;
    	float f;
        } conv;
    
        t0 = random->status[3];
    #if defined(LINEARITY_CHECK)
        t1 = random->status[0]
    	^ (random->status[2] >> TINYMT32_SH8);
    #else
        t1 = random->status[0]
    	+ (random->status[2] >> TINYMT32_SH8);
    #endif
        t0 ^= t1;
        conv.u = ((t0 ^ (-((int32_t)(t1 & 1)) & random->tmat)) >> 9)
    	      | UINT32_C(0x3f800001);
        return conv.f;
    }
    
    /**
     * This function outputs 32-bit unsigned integer from internal state.
     * @param random tinymt internal status
     * @return 32-bit unsigned integer r (0 <= r < 2^32)
     */
    inline static uint32_t tinymt32_generate_uint32(tinymt32_t * random) {
        tinymt32_next_state(random);
        return tinymt32_temper(random);
    }
    
    /**
     * This function outputs floating point number from internal state.
     * This function is implemented using multiplying by 1 / 2^32.
     * floating point multiplication is faster than using union trick in
     * my Intel CPU.
     * @param random tinymt internal status
     * @return floating point number r (0.0 <= r < 1.0)
     */
    inline static float tinymt32_generate_float(tinymt32_t * random) {
        tinymt32_next_state(random);
        return tinymt32_temper(random) * TINYMT32_MUL;
    }
    
    /**
     * This function outputs floating point number from internal state.
     * This function is implemented using union trick.
     * @param random tinymt internal status
     * @return floating point number r (1.0 <= r < 2.0)
     */
    inline static float tinymt32_generate_float12(tinymt32_t * random) {
        tinymt32_next_state(random);
        return tinymt32_temper_conv(random);
    }
    
    /**
     * This function outputs floating point number from internal state.
     * This function is implemented using union trick.
     * @param random tinymt internal status
     * @return floating point number r (0.0 <= r < 1.0)
     */
    inline static float tinymt32_generate_float01(tinymt32_t * random) {
        tinymt32_next_state(random);
        return tinymt32_temper_conv(random) - 1.0f;
    }
    
    /**
     * This function outputs floating point number from internal state.
     * This function may return 1.0 and never returns 0.0.
     * @param random tinymt internal status
     * @return floating point number r (0.0 < r <= 1.0)
     */
    inline static float tinymt32_generate_floatOC(tinymt32_t * random) {
        tinymt32_next_state(random);
        return 1.0f - tinymt32_generate_float(random);
    }
    
    /**
     * This function outputs floating point number from internal state.
     * This function returns neither 0.0 nor 1.0.
     * @param random tinymt internal status
     * @return floating point number r (0.0 < r < 1.0)
     */
    inline static float tinymt32_generate_floatOO(tinymt32_t * random) {
        tinymt32_next_state(random);
        return tinymt32_temper_conv_open(random) - 1.0f;
    }
    
    /**
     * This function outputs double precision floating point number from
     * internal state. The returned value has 32-bit precision.
     * In other words, this function makes one double precision floating point
     * number from one 32-bit unsigned integer.
     * @param random tinymt internal status
     * @return floating point number r (0.0 < r <= 1.0)
     */
    inline static double tinymt32_generate_32double(tinymt32_t * random) {
        tinymt32_next_state(random);
        return tinymt32_temper(random) * (1.0 / 4294967296.0);
    }
    
    #if defined(__cplusplus)
    }
    #endif
    
    #endif
    
    


    /**
     * @file tinymt32.c
     *
     * @brief Tiny Mersenne Twister only 127 bit internal state
     *
     * @author Mutsuo Saito (Hiroshima University)
     * @author Makoto Matsumoto (The University of Tokyo)
     *
     * Copyright (C) 2011 Mutsuo Saito, Makoto Matsumoto,
     * Hiroshima University and The University of Tokyo.
     * All rights reserved.
     *
     * The 3-clause BSD License is applied to this software, see
     * LICENSE.txt
     */
    #include "tinymt32.h"
    #define MIN_LOOP 8
    #define PRE_LOOP 8
    
    /**
     * This function represents a function used in the initialization
     * by init_by_array
     * @param x 32-bit integer
     * @return 32-bit integer
     */
    static uint32_t ini_func1(uint32_t x) {
        return (x ^ (x >> 27)) * UINT32_C(1664525);
    }
    
    /**
     * This function represents a function used in the initialization
     * by init_by_array
     * @param x 32-bit integer
     * @return 32-bit integer
     */
    static uint32_t ini_func2(uint32_t x) {
        return (x ^ (x >> 27)) * UINT32_C(1566083941);
    }
    
    /**
     * This function certificate the period of 2^127-1.
     * @param random tinymt state vector.
     */
    static void period_certification(tinymt32_t * random) {
        if ((random->status[0] & TINYMT32_MASK) == 0 &&
    	random->status[1] == 0 &&
    	random->status[2] == 0 &&
    	random->status[3] == 0) {
    	random->status[0] = 'T';
    	random->status[1] = 'I';
    	random->status[2] = 'N';
    	random->status[3] = 'Y';
        }
    }
    
    /**
     * This function initializes the internal state array with a 32-bit
     * unsigned integer seed.
     * @param random tinymt state vector.
     * @param seed a 32-bit unsigned integer used as a seed.
     */
    void tinymt32_init(tinymt32_t * random, uint32_t seed) {
        random->status[0] = seed;
        random->status[1] = random->mat1;
        random->status[2] = random->mat2;
        random->status[3] = random->tmat;
        int i;
        for (i = 1; i < MIN_LOOP; i++) {
    	random->status[i & 3] ^= i + UINT32_C(1812433253)
    	    * (random->status[(i - 1) & 3]
    	       ^ (random->status[(i - 1) & 3] >> 30));
        }
        period_certification(random);
        for (i = 0; i < PRE_LOOP; i++) {
    	tinymt32_next_state(random);
        }
    }
    
    /**
     * This function initializes the internal state array,
     * with an array of 32-bit unsigned integers used as seeds
     * @param random tinymt state vector.
     * @param init_key the array of 32-bit integers, used as a seed.
     * @param key_length the length of init_key.
     */
    void tinymt32_init_by_array(tinymt32_t * random, uint32_t init_key[],
    			    int key_length) {
        const int lag = 1;
        const int mid = 1;
        const int size = 4;
        int i, j;
        int count;
        uint32_t r;
        uint32_t * st = &random->status[0];
    
        st[0] = 0;
        st[1] = random->mat1;
        st[2] = random->mat2;
        st[3] = random->tmat;
        if (key_length + 1 > MIN_LOOP) {
    	count = key_length + 1;
        } else {
    	count = MIN_LOOP;
        }
        r = ini_func1(st[0] ^ st[mid % size]
    		  ^ st[(size - 1) % size]);
        st[mid % size] += r;
        r += key_length;
        st[(mid + lag) % size] += r;
        st[0] = r;
        count--;
        for (i = 1, j = 0; (j < count) && (j < key_length); j++) {
    	r = ini_func1(st[i % size]
    		      ^ st[(i + mid) % size]
    		      ^ st[(i + size - 1) % size]);
    	st[(i + mid) % size] += r;
    	r += init_key[j] + i;
    	st[(i + mid + lag) % size] += r;
    	st[i % size] = r;
    	i = (i + 1) % size;
        }
        for (; j < count; j++) {
    	r = ini_func1(st[i % size]
    		      ^ st[(i + mid) % size]
    		      ^ st[(i + size - 1) % size]);
    	st[(i + mid) % size] += r;
    	r += i;
    	st[(i + mid + lag) % size] += r;
    	st[i % size] = r;
    	i = (i + 1) % size;
        }
        for (j = 0; j < size; j++) {
    	r = ini_func2(st[i % size]
    		      + st[(i + mid) % size]
    		      + st[(i + size - 1) % size]);
    	st[(i + mid) % size] ^= r;
    	r -= i;
    	st[(i + mid + lag) % size] ^= r;
    	st[i % size] = r;
    	i = (i + 1) % size;
        }
        period_certification(random);
        for (i = 0; i < PRE_LOOP; i++) {
    	tinymt32_next_state(random);
        }
    }
    
    


    Going to be honest, I got no idea how this all works, I just threw darts at a wall as I tried to read through the source of these and understand what did what and how.

My Information

Member Title:
New D.I.C Head
Age:
22 years old
Birthday:
December 26, 1992
Gender:
Programming Languages:
Python, C

Contact Information

E-mail:
Private

Friends

Comments

Akasen has no profile comments yet. Why not say hello?