d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Probability In C
Prev123Next
Add Reply New Topic New Poll
Member
Posts: 1,753
Joined: Apr 12 2011
Gold: 0.00
Mar 25 2013 04:20pm
Quote (Richter @ Mar 25 2013 11:58am)
rand() gives a "random" number.
but if you restart the game, the exyctly same random number will be given by rand() again.
so you would play the same exact game multiple times... wouldn't be that funny...
thats why you need a "seed".
you can set it with srand().
so if you write srand(2) at the start of the game, it will be different than the game, where you write srand(1) at the start of the game.
but still you got the problem, that the games with the same number 'x' inside of srand(x) will be the same game as srand(x).
so if you put inside srand() the time, the games will be different of each other, if the time-function's result changes between the games.

the function rand() repeats every xxxxxxxxxx (i dont know how long the sequence actually is)
thats why it's also called a pseuro random generator
with the right math you could get the state of this random generator.
with the right state, you can predict the 'future' states of the generator.
but you need enough data for that.
you can decrypt WEP-encrypted-wifis with that....

so to your problem again, you get a number between 1 and a huge number. if you make modulo 10, you get numbers between 0 and 9. you only get hte 0 in one case of the 10 possible cases, thats where the 10% come from.


ah great explanation. thank you

Quote (AbDuCt @ Mar 25 2013 10:08am)
heh. im still trying to figure out how to present the next part. its easy enough commenting code and explaining it over all but the methods of finding what i needed to know in order to create the code (ex grabbing memory addresses/pointers/addresses of machine code) is beyond what screenshots and text and do really.


meh i'm gonna dread that...and figuring out how to do the display >.>
Member
Posts: 16,144
Joined: Mar 27 2008
Gold: 14,618.00
Mar 25 2013 05:10pm
Quote (Reverence @ 25 Mar 2013 23:20)
and figuring out how to do the display >.>

maybe like this?
Code
for (int i=0; i<100; i++) {
printf("The random number was %d\n", yourRandomFunc());
}
Member
Posts: 1,753
Joined: Apr 12 2011
Gold: 0.00
Mar 25 2013 05:41pm
i wasn't talking about that lol. i was talking about the game



i want to learn how to keep a sort of static display - i don't know what that's really called but rather than just printing new lines when something new happens, what was displayed previously is updated (eg moving from "fight" to "bag", the display wouldn't change positions, it would just update and only the arrow would be in a new position)
and ofc i need to learn how to make the display period

This post was edited by Reverence on Mar 25 2013 05:44pm
Member
Posts: 16,144
Joined: Mar 27 2008
Gold: 14,618.00
Mar 25 2013 07:08pm
ah lol okey :)

well, you just need to save everything that must appear on the screen in the right datatype. everytime when you want to update the display, you only update the datatype. after that you clear the screen and redraw everything that you saved. ;)

i think you want a console version of it? or are you already working at a image-based version?
Member
Posts: 1,753
Joined: Apr 12 2011
Gold: 0.00
Mar 25 2013 07:27pm
Quote (Richter @ Mar 25 2013 03:08pm)
ah lol okey :)

well, you just need to save everything that must appear on the screen in the right datatype. everytime when you want to update the display, you only update the datatype. after that you clear the screen and redraw everything that you saved. ;)

i think you want a console version of it? or are you already working at a image-based version?


yeah, i've only taken a semester of C and that was a very basic class lol. the final project was a sort of game, and the only thing that was given to us was the display, though we weren't even told how it worked.

i think there are proper resources/graphics available but i want to mess around with it this way before moving on to that, if i ever do
Member
Posts: 16,144
Joined: Mar 27 2008
Gold: 14,618.00
Mar 25 2013 07:42pm
you want a two-dimensional array of ascii chars? where you can walk around?
maybe something like nethack?


if yes, create a 2-d array:
#define xsize 100
#define ysize 60
char data[xsize][ysize];
(maybe you also want to use memset to initialize the array)
Member
Posts: 1,753
Joined: Apr 12 2011
Gold: 0.00
Mar 26 2013 03:39am
uh...i'll think about that later lol

right now i want to figure out how to store/reference my data. character strings for monster/skill names, and integers for monster/skill stats

the only way i know how to do this id by making a 2d array for monster ids/monster stats and skill ids, and another array for skill ids/skill stats. not sure how to treat the names...but for the int arrays, i would need to reference nearly two entire columns from each of them after every round (battle calculations for player 1 and player 2), as well as other data unique to the current fight. are there other methods i should look in to that are cleaner or more efficient than what i have in mind?
Member
Posts: 16,144
Joined: Mar 27 2008
Gold: 14,618.00
Mar 26 2013 04:54am
you need to learn structs imo.

make a struct for a "monster", then make an array including all monsters. (1d array is enough, 2d is overkill in this case)

lets assume we want 10 monsters, so we create an array of such a monster. arraysize would be then 10 * sizeof(monsterstruct)
all you need to do is setup 1 struct for a monster, with stats, damage, hp, etc.
then you make an array. code:

Code
*define the struct for one monster here*

monster_t monsters[10]; //and here you create 10 monsters


only problem you got now is how you save the "monstername" in a struct? a string can have various lengths... a struct is always the same lengths.
either you implement that with memory allocations and save only the pointer in the struct (i guess you don't want to do that, it's more complicated and usually not worth it). therefore you have to define the maximum length for a monstername... 30chars maybe? so in your struct, you make a character array of 31 chars (the last character is the 0x00 terminator, which indicates that the string ends)

This post was edited by Richter on Mar 26 2013 04:55am
Member
Posts: 1,753
Joined: Apr 12 2011
Gold: 0.00
Mar 26 2013 03:16pm
read a bit on structs. right now what i'm thinking is that i'll have two sets of structs which would basically look like this, only with a lot more stats:

for monsters:

-monster id
-monster name
-hp value
-att value
-1st skill id
-2nd skill id
-3rd skill id

for skills:

-skill id
-skill name
-dmg value


but if i do it this way, i'd need to add a function to detect white space whenever i want to display a name
Member
Posts: 16,144
Joined: Mar 27 2008
Gold: 14,618.00
Mar 26 2013 04:04pm
why do you need a function which detects a white space?

and which "display-function" you want to use?

please also note this:
Code
#include <stdio.h>
void main(void) {
 char asdf1[3] = "Hi";
 char asdf2[3] = {'H', 'i', 0};

 char asdf3[5] = "Hooo";
 char asdf4[5] = {'H', 'o', 'o', 'o'};

 char asdf5[5] = "Hi";
 char asdf6[5] = {'H', 'i', 0, 'x', 'w'};

 printf("asdf1: %s\n",asdf1);
 printf("asdf2: %s\n",asdf2);
 printf("asdf3: %s\n",asdf3);
 printf("asdf4: %s\n",asdf4);
 printf("asdf5: %s\n",asdf5);
 printf("asdf6: %s\n",asdf6);
}


minimum size for saving 2 chars as a string is 3 bytes, if you want to use printf to display the string. if you forget the terminating null byte, the behaviour may be unexpected.

minimum size for saving 4 chars as a string is 5 bytes. the last one should be zero. if you write the string with doubled quotes, the compiler automatically adds the zero.

if you have too many chars but you dont need them all, it doesn't matter what you have after the first zero.

also note, that the whitespace is not the zero (look at ascii). if you want to add a whitespace to the monstername, just replace the letter with ' ' (those single quotes are required, lol). you could also insert the value 32

This post was edited by Richter on Mar 26 2013 04:08pm
Go Back To Programming & Development Topic List
Prev123Next
Add Reply New Topic New Poll