Answer» I have a program that I am tinkering on and it has 8 seeded random generators. I am trying to fix 2 problems.
First problem is that unless I put in a delay between the first group of 4 seeded random generators and the second group, both sets of 4 will output the same values. By placing a loop between the first group and the second group I end up with a different output because time was taken to process that loop so the seed changed for the next group of 4 seeded randoms. If you coment out:
Code: [Select] for(Delay=1;Delay<=2000;Delay++){ cout<<"\n"; }you can then see how they end up being same.
Second problem is that it appears that the first group of 4 seeded randoms seem to be of a value less than the second group of 4, so I am thinking that I MAY be dealing with an issue with riding the algorithm to where the second group processed has an unfair advantage than the first group. Ran it 8 times and kept getting results that for a game would give Player 2 an unfair advantage than Player 1, even though the code is exactly the same for both players. Thinking I might need a more advanced seeded random generator or to randomize who gets Player 1 and Player 2's outputs which just seems like a band aid to a better way to do this that someone might be able to point out to me.
Code: [Select]#include<iostream> #include<ctime> // For time() #include<cstdlib> // For srand() and rand() using namespace std;
// =================== GAME VALUES =========================================== int P1_Max_Value;//GAME Static Crit Comparitor Initialization int P1_B=22; //GAME Static Crit Stat...Never Changed from 22 int P1_X=0; //GAME Dynamic Bonus Damage on Crit 0 = NONE ... SEE [ W=(D*.01)*(A+X) ] int P1_Max_Hit; //GAME Static Hit Comparitor Initialization int P1_CX=11; //GAME Hit - 10% Chance to Miss 11 when calc against 1 to 11 int P1_Hit=0; //GAME Hit or Miss 1=Hit 0=Miss int P1_Block=0; //GAME BLOCK or Get Struck
int Delay; //DELAY
int P2_Max_Value;//GAME Static Crit Comparitor Initialization int P2_B=22; //GAME Static Crit Stat...Never Changed from 22 int P2_X=0; //GAME Dynamic Bonus Damage on Crit 0 = NONE ... SEE [ W=(D*.01)*(A+X) ] int P2_Max_Hit; //GAME Static Hit Comparitor Initialization int P2_CX=11; //GAME Hit - 10% Chance to Miss 11 when calc against 1 to 11 int P2_Hit=0; //GAME Hit or Miss 1=Hit 0=Miss int P2_Block=0; //GAME Block or Get Struck
int P1_A=100; //PLAYER Dynamic Weapon Damage Value int P1_BE=1; //PLAYER Dynamic Crit Stat int P1_D=100; //PLAYER Dynamic Weapon Durability where 100 is 100% and 99 is 99% etc int P1_CE=0; //PLAYER Dynamic Hit int P1_EE=0; //PLAYER Dynamic Block
int P2_A=100; //PLAYER Dynamic Weapon Damage Value int P2_BE=1; //PLAYER Dynamic Crit Stat int P2_D=100; //PLAYER Dynamic Weapon Durability where 100 is 100% and 99 is 99% etc int P2_CE=0; //PLAYER Dynamic Hit int P2_EE=0; //PLAYER Dynamic Block
int P1_RA=0; //RANDOM Calc Weapon Damage Output Value int P1_RB=0; //RANDOM Calc Crit Bonus Damage Value int P1_RC=0; //RANDOM Calc Hit Value int P1_RE=0; //RANDOM Calc Block Value
int P2_RA=0; //RANDOM Calc Weapon Damage Output Value int P2_RB=0; //RANDOM Calc Crit Bonus Damage Value int P2_RC=0; //RANDOM Calc Hit Value int P2_RE=0; //RANDOM Calc Block Value
int P1_C=20; //OUTPUT Dynamic Hit [ 1 = Hit & 0 = Miss ] int P1_E=20; //OUTPUT Dynamic Block [ 1 = Block & 0 = Miss ] double P1_W=0; //OUTPUT Dynamic Weapon Damage Output + Crit Bonus Damage
int P2_C=20; //OUTPUT Dynamic Hit [ 1 = Hit & 0 = Miss ] int P2_E=20; //OUTPUT Dynamic Block [ 1 = Block & 0 = Miss ] double P2_W=0; //OUTPUT Dynamic Weapon Damage Output + Crit Bonus Damage
double HP_Player1=1000; //Health Player 1 double HP_Player2=1000; //Health Player 2
// A = Weapon Max DPS rating // ----------------- // B = Crit Calc // BE = Players Crit Value... by lowering 22, the crit chance changes from 4.54% to 4.76% etc // ----------------- // CRIT TABLE -Player Stat 'BE' Starting at 0 and working towards 21 21 each swing has 100% weapon damage output // ----------------- // BE=0 = B=22 = 4.545% // BE=1 = B=21 = 4.761% // BE=2 = B=20 = 5.000% // BE=3 = B=19 = 5.263% // BE=4 = B=18 = 5.555% // BE=5 = B=17 = 5.882% // BE=6 = B=16 = 6.250% // BE=7 = B=15 = 6.666% // BE=8 = B=14 = 7.142% // BE=9 = B=13 = 7.692% // BE=10 = B=12 = 8.333% // BE=11 = B=11 = 9.090% // BE=12 = B=10 = 10.000% // BE=13 = B=09 = 11.111% // BE=14 = B=08 = 12.500% // BE=15 = B=07 = 14.285% // BE=16 = B=06 = 16.666% // BE=17 = B=05 = 20.000% // BE=18 = B=04 = 25.000% // BE=19 = B=03 = 33.333% // BE=20 = B=02 = 50.000% // BE=21 = B=01 = 100.000% // ----------------- // C = Hit or Miss // D = Durability // E = Defense ( Block/Dodge ) /*Offensive Formulas
Calculate Damage Out { (100) Max DPS * (.5) Random Output Damage Multiplier, where output of 0 = miss = (50) RawDPS + (10) DPS Enhancement for (2) strikes -- until Estrikes = 0 and if a crit is reached by the Random Generator Calculating chance to crit, an output of 100% damage + Estrike damage is unleashed.
[A] MaxDPS * SeededRandom 0 to 10 = Weapon Damage Potential (100 * 5) = ([500] / 10) = [50] Bonus Damage for X many strikes + WDP = Total Weapon Damage 10 for next 2 turns + 50 = [60]
[B] Crit Formula Crit STATS shorten the gap in the seeded random generator so its more probable for a crit to happen. If a crit event triggers then software ignores the WDP and goes with Maximum DPS + Bonus Damage for a ptential of say 110 damage by using the numbers shown above. *There is a 1 in 22 chance to crit without crit stat enhancements. Crit stat enhancements drop this to a maximum of 1 in 4.
[C] Hit Rating Hit rating decides if you hit or miss. By default stats you have a 20% chance of missing. The formula is that seeded random from 1 to H where H = 5. If say you used the middle of 1 and 5 of 3 as the trigger for miss through calculation of ( (1+5)/2)=3. If the seeded value was equal to 3 then no matter what [A] or [B] came out to be, you still missed causing no damage.
*/
int main() { //+++++++++ //PLAYER 1 //+++++++++ P1_Max_Value=P1_B; //Crit Comparitor Setup... Used Below in Testing for Max Crit = Max Weapon Damage P1_B =(P1_B-P1_BE); //Players Crit Enhancer 'BE' affecting Crit of 'B' // As 'BE' Players Crit increases in value the chance for a crit increases
// ========================= DAMAGE + CRIT + DURABILITY ======================================= // Formula for DPS Out srand(time(0)); // Initialize seeded random number generator. P1_RA=(rand()%P1_A)+1; // Create Seeded Random Output
// Formula for Crit Chance // Bonus Damage, but unleash 100% weapon damage if crit = max value
srand(time(0)); // Initialize random number generator. P1_RB=(rand()%P1_B)+1; // Create Seeded Random Output // ========================= // Formula for Hit Out srand(time(0)); // Initialize seeded random number generator. P1_RC=(rand()%P1_C)+1; // Create Seeded Random Output if(P1_RC==P1_C){ //Miss when equal to P1_Hit=0; } else { // If RC Not Equal to C then its a Hit as long as opponent doesnt block or dodge P1_Hit=1; } // ========================= // Formula for Block Out srand(time(0)); // Initialize seeded random number generator. P1_RE=(rand()%P1_E)+1; // Create Seeded Random Output if(P1_RE==P1_E){ //Block when equal to P1_Block=1; } else { // If RE Not Equal to E then its a Hit as long as opponent doesnt block or dodge P1_Block=0; }
// Conditional Output Below if(P1_RB==P1_B){ // Maximum Weapon Damage - Durability Loss P1_W=(P1_D*.01)*(P1_A+P1_X);// Maximum Weapon Damage Output if crit max value is reached // Later on (A+X) can be changed to replace X with say an additional bonus damage } else{
//W=((D*.01)*(RA+(RA*(RB*.01)))); // Original Formula P1_W=((P1_D*.01)*(P1_RA+(P1_RA*(P1_RB*.01)))); // Current Formula in use //W=((100*.01)*(100+(100*(21*.01)))); // Max Test
// Weapon + Crit Restriction... Make always less than Weapon Max // Without Weapon + Restriction ... Damage = 121 on a 100 weapon if(P1_W>=P1_A){ P1_W=(P1_A-1); // Make Weapon Damage = Less than Crit of Max Damage } } // =================================================================================== //same seed issue corrected by delay for(Delay=1;Delay<=2000;Delay++){ cout<<"\n"; }
//+++++++++ //PLAYER 2 //+++++++++
P2_Max_Value=P2_B; //Crit Comparitor Setup... Used Below in Testing for Max Crit = Max Weapon Damage P2_B =(P2_B-P2_BE); //Players Crit Enhancer 'BE' affecting Crit of 'B' // As 'BE' Players Crit increases in value the chance for a crit increases
// ========================= DAMAGE + CRIT + DURABILITY ======================================= // Formula for DPS Out srand(time(0)); // Initialize seeded random number generator. P2_RA=(rand()%P2_A)+1; // Create Seeded Random Output
// Formula for Crit Chance // Bonus Damage, but unleash 100% weapon damage if crit = max value
srand(time(0)); // Initialize random number generator. P2_RB=(rand()%P2_B)+1; // Create Seeded Random Output // ========================= // Formula for Hit Out srand(time(0)); // Initialize seeded random number generator. P2_RC=(rand()%P2_C)+1; // Create Seeded Random Output if(P2_RC==P2_C){ //Miss when equal to P2_Hit=0; } else { // If RC Not Equal to C then its a Hit as long as opponent doesnt block or dodge P2_Hit=1; } // ========================= // Formula for Block Out srand(time(0)); // Initialize seeded random number generator. P2_RE=(rand()%P2_E)+1; // Create Seeded Random Output if(P2_RE==P2_E){ //Block when equal to P2_Block=1; } else { // If RE Not Equal to E then its a Hit as long as opponent doesnt block or dodge P2_Block=0; }
// Conditional Output Below if(P2_RB==P2_B){ // Maximum Weapon Damage - Durability Loss P2_W=(P2_D*.01)*(P2_A+P2_X);// Maximum Weapon Damage Output if crit max value is reached // Later on (A+X) can be changed to replace X with say an additional bonus damage } else{
//W=((D*.01)*(RA+(RA*(RB*.01)))); // Original Formula P2_W=((P2_D*.01)*(P2_RA+(P2_RA*(P2_RB*.01)))); // Current Formula in use //W=((100*.01)*(100+(100*(21*.01)))); // Max Test
// Weapon + Crit Restriction... Make always less than Weapon Max // Without Weapon + Restriction ... Damage = 121 on a 100 weapon if(P2_W>=P2_A){ P2_W=(P2_A-1); // Make Weapon Damage = Less than Crit of Max Damage } }
// Below Test Display for development Purpose Only..[ Comment Out ] cout<<"Player 1 HP == "<<HP_Player1<<"\n"; cout<<"Weapon Output 100 == "<<P1_RA<<"\n"; // Test Display of Output Value cout<<"Crit Output Max 22 == "<<P1_RB<<"\n"; // Test Display of Output Value cout<<"Weapon Output Calc == "<<P1_W<<"\n"; // Test Display of Output Value cout<<"Hit == "<<P1_Hit<<"\n"; // Test Display of Output Value cout<<"Block == "<<P1_Block<<"\n";// Test Display of Output Value cout<<"\n"; cout<<"Player 2 HP == "<<HP_Player2<<"\n"; cout<<"Weapon Output 100 == "<<P2_RA<<"\n"; // Test Display of Output Value cout<<"Crit Output Max 22 == "<<P2_RB<<"\n"; // Test Display of Output Value cout<<"Weapon Output Calc == "<<P2_W<<"\n"; // Test Display of Output Value cout<<"Hit == "<<P2_Hit<<"\n"; // Test Display of Output Value cout<<"Block == "<<P2_Block<<"\n";// Test Display of Output Value
return(0); } Also to note the code may have some extra INT's declared etc as I tinker on this off and on and had added and removed code and havent performed a clean up yet.
Was thinking someone may be able to show a more complex seeded method that will fix both issues.Problem Solved ... If the seed is inside a loop it will repeat, and if seed is outside a loop it does not repeat. This fixed both problems such as tested in this that I played with today by moving stuff around.
Code: [Select]#include <iostream> #include <stdio.h> #include <stdlib.h> #include <time.h> using namespace std; int pause,i,a,b;
int main () { srand ( time(0) ); for(i=1;i<=20;i++){ a=rand() % 100; b=rand() % 100; cout<<"First number: "<<a<<"\n"; cout<<"Second number:"<<b<<"\n"; } cin>>pause; return 0; }The issue with the delay is simply this:
Code: [Select]srand(time(0)); // Initialize random number generator. You are using the system clock as the source of the seed. If you make repeated reads of time(0) sufficiently close together you are using the same seed each time. Since time(0) changes once per second, you need to wait at least that long to be sure of a new seed value.
One should only seed the random number generator once. seeding it multiple times doesn't make the values more random.I guess everyone knows you can get random numbers from random.org using a GET request. A suitable CLIENT is wget or there is curl etc
Using cmd you have to escape the ampersands...
full syntax info at random.org. Play nice with requests or they can ban you (read the guidance about automated clients)
Code: [Select]C:\Users\Me>wget http://www.random.org/integers/?num=10^&min=1^&max=100^&col=1^&base=10^&format=plain^&rnd=new -O rand.txt --2011-09-01 18:55:40-- http://www.random.org/integers/?num=10&min=1&max=100&col=1&base=10&format=plain&rnd=new Resolving www.random.org... 174.143.173.125 Connecting to www.random.org|174.143.173.125|:80... connected. HTTP request sent, awaiting response... 200 OK Length: 29 [text/plain] Saving to: `rand.txt'
100%[========================================================>] 29
2011-09-01 18:55:40 (1.17 MB/s) - `rand.txt' saved [29/29]
C:\Users\Me>type rand.txt 31 12 66 14 1 70 13 36 45 89
C:\Users\Me>Thanks for the INFORMATION BC and Salmon, also I wasnt aware of the random generator online at random.org thats neat.
Do you know what they use that is better than a 1 second seed? Is there a way to seed milliseconds vs seconds to have it change more frequent? All references I see online are in seconds for seeding because I guess people dont need to change it that often.
Currently calling the seed once before the group of randoms I an getting better results that I can work with. I realize now that calling the seed for each random was basically reinitializing the rand function algorithm, making rand act as if the program was running as rand vs srand and giving same output as if you started and stopped an unseeded random and you get the same pattern, and thats why I was getting the repeats, as well as why I was getting numbers I didnt like between 2 players in a game I am tinkering on.
Quote from: DaveLembke on September 01, 2011, 12:25:15 PM Thanks for the information BC and Salmon, also I wasnt aware of the random generator online at random.org thats neat.
Do you know what they use that is better than a 1 second seed? Is there a way to seed milliseconds vs seconds to have it change more frequent? All references I see online are in seconds for seeding because I guess people dont need to change it that often.
According to the site, it's based on atmospheric aberrations.
You should NOT seed a random number generator multiple times in a run. It reduces the entropy of the results, it doesn't increase them.
|