random seeds?
-
hi there, im new to the forums. Im creating a dice game. however my attack logic is some what flawed. I was having trouble with random numbers generating the same number so i placed them inside there own class hoping it would then generate diffrently. however now the same number generates eg 66 + 66. this is a rather pain as my whole game will contain about 7 randoms.All being called ont he same button.Another problem im having (///////second code///////) Where the random only generates and pulls up if (dilemma == 1). Just a note im fairly new to c# so if you could explain any fixes you come up with i'd really appreciate it. regards ant
public class Globals4
{
public static int user;
public static Random uc = new Random();
}public class Globals3
{
public static int comp;
public static Random cc = new Random();
}lblwarning.Text = "Warning";
MessageBox.Show("personal attack");
int userresult = Globals4.user + Globals.army;
Globals3.comp = Globals3.cc.Next(1, 100);
Globals4.user = Globals4.uc.Next(1, 100);
Globals4.user = Globals4.user + Globals.army;
MessageBox.Show("Computer rolled " + Globals3.comp + " You rolled " + Globals4.user + " with an army strength of " + Globals.army + " equals " + userresult + "");//////////////////////////////SECOND CODE//////////////////////////////////////
dilemma = dl.Next(1, 1);
DialogResult res;
if (dilemma == 1)
{res = MessageBox.Show("marriage", "Dilemma", MessageBoxButtons.YesNo); if (res == DialogResult.Yes) { Globals.notoriety -= 1; } else if (res == DialogResult.No) { Globals.notoriety += 1; } if (Dilema == 2) { res = MessageBox.Show(" force?", "check", MessageBoxButtons.YesNo); if (res == DialogResult.Yes) { Globals.notoriety -= 1; } else if (res == DialogResult.No) { Globals.notoriety += 1; }
//EDIT i forgot to add this is c# visual i believe its called .net (not the console)
-
hi there, im new to the forums. Im creating a dice game. however my attack logic is some what flawed. I was having trouble with random numbers generating the same number so i placed them inside there own class hoping it would then generate diffrently. however now the same number generates eg 66 + 66. this is a rather pain as my whole game will contain about 7 randoms.All being called ont he same button.Another problem im having (///////second code///////) Where the random only generates and pulls up if (dilemma == 1). Just a note im fairly new to c# so if you could explain any fixes you come up with i'd really appreciate it. regards ant
public class Globals4
{
public static int user;
public static Random uc = new Random();
}public class Globals3
{
public static int comp;
public static Random cc = new Random();
}lblwarning.Text = "Warning";
MessageBox.Show("personal attack");
int userresult = Globals4.user + Globals.army;
Globals3.comp = Globals3.cc.Next(1, 100);
Globals4.user = Globals4.uc.Next(1, 100);
Globals4.user = Globals4.user + Globals.army;
MessageBox.Show("Computer rolled " + Globals3.comp + " You rolled " + Globals4.user + " with an army strength of " + Globals.army + " equals " + userresult + "");//////////////////////////////SECOND CODE//////////////////////////////////////
dilemma = dl.Next(1, 1);
DialogResult res;
if (dilemma == 1)
{res = MessageBox.Show("marriage", "Dilemma", MessageBoxButtons.YesNo); if (res == DialogResult.Yes) { Globals.notoriety -= 1; } else if (res == DialogResult.No) { Globals.notoriety += 1; } if (Dilema == 2) { res = MessageBox.Show(" force?", "check", MessageBoxButtons.YesNo); if (res == DialogResult.Yes) { Globals.notoriety -= 1; } else if (res == DialogResult.No) { Globals.notoriety += 1; }
//EDIT i forgot to add this is c# visual i believe its called .net (not the console)
Random does not produce actually random values, it's a pseudo-random generator. Seed your Random with time related value. Something like that
new Random(DateTime.Now.GetHashCode());
ornew Random(Environment.TickCount);
Eslam Afifi
-
Random does not produce actually random values, it's a pseudo-random generator. Seed your Random with time related value. Something like that
new Random(DateTime.Now.GetHashCode());
ornew Random(Environment.TickCount);
Eslam Afifi
im afraid i dont really understand could you mock me up an example of 2 randoms in one method?
-
hi there, im new to the forums. Im creating a dice game. however my attack logic is some what flawed. I was having trouble with random numbers generating the same number so i placed them inside there own class hoping it would then generate diffrently. however now the same number generates eg 66 + 66. this is a rather pain as my whole game will contain about 7 randoms.All being called ont he same button.Another problem im having (///////second code///////) Where the random only generates and pulls up if (dilemma == 1). Just a note im fairly new to c# so if you could explain any fixes you come up with i'd really appreciate it. regards ant
public class Globals4
{
public static int user;
public static Random uc = new Random();
}public class Globals3
{
public static int comp;
public static Random cc = new Random();
}lblwarning.Text = "Warning";
MessageBox.Show("personal attack");
int userresult = Globals4.user + Globals.army;
Globals3.comp = Globals3.cc.Next(1, 100);
Globals4.user = Globals4.uc.Next(1, 100);
Globals4.user = Globals4.user + Globals.army;
MessageBox.Show("Computer rolled " + Globals3.comp + " You rolled " + Globals4.user + " with an army strength of " + Globals.army + " equals " + userresult + "");//////////////////////////////SECOND CODE//////////////////////////////////////
dilemma = dl.Next(1, 1);
DialogResult res;
if (dilemma == 1)
{res = MessageBox.Show("marriage", "Dilemma", MessageBoxButtons.YesNo); if (res == DialogResult.Yes) { Globals.notoriety -= 1; } else if (res == DialogResult.No) { Globals.notoriety += 1; } if (Dilema == 2) { res = MessageBox.Show(" force?", "check", MessageBoxButtons.YesNo); if (res == DialogResult.Yes) { Globals.notoriety -= 1; } else if (res == DialogResult.No) { Globals.notoriety += 1; }
//EDIT i forgot to add this is c# visual i believe its called .net (not the console)
Hi, Are you by any chance a graduate of the school of "I'm absolutely not going to read the documentation" programming? The reason I ask is that the title of your original post was the answer, i.e you knew that you wanted to seed the random number generator. When you were told how to do it, you asked for code. I have just opened the documentation for the Random class and on the introductory page is a full example showing more than one method of seeding the generator. If you had just done the same then your problem would have been solved in less time than it took to post the question. Alan.
-
hi there, im new to the forums. Im creating a dice game. however my attack logic is some what flawed. I was having trouble with random numbers generating the same number so i placed them inside there own class hoping it would then generate diffrently. however now the same number generates eg 66 + 66. this is a rather pain as my whole game will contain about 7 randoms.All being called ont he same button.Another problem im having (///////second code///////) Where the random only generates and pulls up if (dilemma == 1). Just a note im fairly new to c# so if you could explain any fixes you come up with i'd really appreciate it. regards ant
public class Globals4
{
public static int user;
public static Random uc = new Random();
}public class Globals3
{
public static int comp;
public static Random cc = new Random();
}lblwarning.Text = "Warning";
MessageBox.Show("personal attack");
int userresult = Globals4.user + Globals.army;
Globals3.comp = Globals3.cc.Next(1, 100);
Globals4.user = Globals4.uc.Next(1, 100);
Globals4.user = Globals4.user + Globals.army;
MessageBox.Show("Computer rolled " + Globals3.comp + " You rolled " + Globals4.user + " with an army strength of " + Globals.army + " equals " + userresult + "");//////////////////////////////SECOND CODE//////////////////////////////////////
dilemma = dl.Next(1, 1);
DialogResult res;
if (dilemma == 1)
{res = MessageBox.Show("marriage", "Dilemma", MessageBoxButtons.YesNo); if (res == DialogResult.Yes) { Globals.notoriety -= 1; } else if (res == DialogResult.No) { Globals.notoriety += 1; } if (Dilema == 2) { res = MessageBox.Show(" force?", "check", MessageBoxButtons.YesNo); if (res == DialogResult.Yes) { Globals.notoriety -= 1; } else if (res == DialogResult.No) { Globals.notoriety += 1; }
//EDIT i forgot to add this is c# visual i believe its called .net (not the console)
As I have understood your post, you are having troubles with random numbers generation. Here is a solution, that may be helpful. As it has been noted, a seed number is used to start numbers generation. In most occasions this number is dependent on current time, for example, Environment.TickCount can be such a seed. What happens sometimes, is that number sequences are being generated too fast, so that the time-related seed is not changed. This may result in troubles, when you are generating numbers not in one sequence. Here is a code example. We are generating an array of objects typed
sample
, and each object contains a set of randomly generated numbers.class sample
{
double[] dblMyArray = new double[20]; // number 20 bears no actual meaning, it's just an examplepublic sample() { Random rndObj = new Random(Environment.TickCount); for (int i = 0; i < dblMyArray.Length; i++) dblMyArray\[i\] = rndObj.NextDouble(); } } class Program { static void Main(string\[\] args) { sample\[\] mainArray = new sample\[10\]; // number 10 bears no actual meaning, it's just an example for (int i = 0; i < mainArray.Length; i++) mainArray\[i\] = new sample(); } }
Now, what will happen is a sad fact: all the objects in
mainArray
will have THE SAME values indblMyArray
. This is because modern computations are fast, and time related seed cannot change fast enough. You may run this code and check out the values. If you have a similar problem, the solution is to sleep a program for a short time, I used 1ms. This way the seed number will change, and each time you'll have different sequences. -
Hi, Are you by any chance a graduate of the school of "I'm absolutely not going to read the documentation" programming? The reason I ask is that the title of your original post was the answer, i.e you knew that you wanted to seed the random number generator. When you were told how to do it, you asked for code. I have just opened the documentation for the Random class and on the introductory page is a full example showing more than one method of seeding the generator. If you had just done the same then your problem would have been solved in less time than it took to post the question. Alan.
well that was a nice response,I presume your from the old school of elitist programmers who bash new people to a language or just say try google Considering i've been looking into this for 3 days hence why i knew what i was doing just not sure how to impliment it and not really understooding the code thats why i posted. I also asked for a interpretation of the code as a general rule to work from not for someone to do it for me. Im sorry ive my lack of knowledge causes you offence but as a rule of thumb forums are to learn from
-
As I have understood your post, you are having troubles with random numbers generation. Here is a solution, that may be helpful. As it has been noted, a seed number is used to start numbers generation. In most occasions this number is dependent on current time, for example, Environment.TickCount can be such a seed. What happens sometimes, is that number sequences are being generated too fast, so that the time-related seed is not changed. This may result in troubles, when you are generating numbers not in one sequence. Here is a code example. We are generating an array of objects typed
sample
, and each object contains a set of randomly generated numbers.class sample
{
double[] dblMyArray = new double[20]; // number 20 bears no actual meaning, it's just an examplepublic sample() { Random rndObj = new Random(Environment.TickCount); for (int i = 0; i < dblMyArray.Length; i++) dblMyArray\[i\] = rndObj.NextDouble(); } } class Program { static void Main(string\[\] args) { sample\[\] mainArray = new sample\[10\]; // number 10 bears no actual meaning, it's just an example for (int i = 0; i < mainArray.Length; i++) mainArray\[i\] = new sample(); } }
Now, what will happen is a sad fact: all the objects in
mainArray
will have THE SAME values indblMyArray
. This is because modern computations are fast, and time related seed cannot change fast enough. You may run this code and check out the values. If you have a similar problem, the solution is to sleep a program for a short time, I used 1ms. This way the seed number will change, and each time you'll have different sequences.i just mocked up mine i have a feelings its wrong but cant test atm
public class Globals2
{
double[] dblMyArray = new double[20];
}public class Globals3
{
public static int comp;
public static Random cc = new Random(Environment.TickCount);
for (int i = 0; i < dblMyArray.Length; i++)
dblMyArray[i] = cc.NextDouble();
}public class Globals4
{
public static int comp;
public static Random uc = new Random(Environment.TickCount);
for (int i = 0; i < dblMyArray.Length; i++)
dblMyArray[i] = cc.NextDouble();
}//main program
lblwarning.Text = "Warning";
MessageBox.Show("personal attack");
int userresult = Globals4.user + Globals.army;
sample[] mainArray = new sample[10];
for (int i = 0; i < mainArray.Length; i++)
mainArray[i] = new sample();Globals3.comp = Globals3.cc.Next(1, 100);
Thread.Sleep(10);
sample[] mainArray = new sample[10];
for (int i = 0; i < mainArray.Length; i++)
mainArray[i] = new sample();Globals4.user = Globals4.uc.Next(1, 100);
Globals4.user = Globals4.user + Globals.army;
MessageBox.Show("Computer rolled " + Globals3.comp + " You rolled " + Globals4.user + " with an army strength of " + Globals.army + " equals " + userresult + ""); -
well that was a nice response,I presume your from the old school of elitist programmers who bash new people to a language or just say try google Considering i've been looking into this for 3 days hence why i knew what i was doing just not sure how to impliment it and not really understooding the code thats why i posted. I also asked for a interpretation of the code as a general rule to work from not for someone to do it for me. Im sorry ive my lack of knowledge causes you offence but as a rule of thumb forums are to learn from
Hi, A lack of knowledge does not offend me in the slightest and I am more than willing to help. The point I made was that you used "Random seeds?" for the title of your post yet you appeared not to have done any research on that very topic. You must have thought it a bit odd that something called a random number generator was giving the same sequence every time it was used. Surely the place to start was with the documentation. Alan.
-
i just mocked up mine i have a feelings its wrong but cant test atm
public class Globals2
{
double[] dblMyArray = new double[20];
}public class Globals3
{
public static int comp;
public static Random cc = new Random(Environment.TickCount);
for (int i = 0; i < dblMyArray.Length; i++)
dblMyArray[i] = cc.NextDouble();
}public class Globals4
{
public static int comp;
public static Random uc = new Random(Environment.TickCount);
for (int i = 0; i < dblMyArray.Length; i++)
dblMyArray[i] = cc.NextDouble();
}//main program
lblwarning.Text = "Warning";
MessageBox.Show("personal attack");
int userresult = Globals4.user + Globals.army;
sample[] mainArray = new sample[10];
for (int i = 0; i < mainArray.Length; i++)
mainArray[i] = new sample();Globals3.comp = Globals3.cc.Next(1, 100);
Thread.Sleep(10);
sample[] mainArray = new sample[10];
for (int i = 0; i < mainArray.Length; i++)
mainArray[i] = new sample();Globals4.user = Globals4.uc.Next(1, 100);
Globals4.user = Globals4.user + Globals.army;
MessageBox.Show("Computer rolled " + Globals3.comp + " You rolled " + Globals4.user + " with an army strength of " + Globals.army + " equals " + userresult + "");just got round to trying it with no luck
-
just got round to trying it with no luck
just an update i got it working with the threadtime scheduling thanks for the tip however the 2nd bit of code still isnt working so i placed it in its own fucntion
/ rolling function
public class Globals6
{public static int dilemma = 0; public void random1() { Random dl = new Random(Environment.TickCount); Globals5.dilemma = dl.Next(1, 2); DialogResult res; if (Globals5.dilemma == 1) { res = MessageBox.Show("Ban same sex marriage", "Dilemma", MessageBoxButtons.YesNo); if (res == DialogResult.Yes) { Globals.notoriety -= 1; } else if (res == DialogResult.No) { Globals.notoriety += 1; } if (Globals5.dilemma == 2) { res = MessageBox.Show("Political protest, do you use essesive force?", "check", MessageBoxButtons.YesNo); if (res == DialogResult.Yes) { Globals.notoriety -= 1; } else if (res == DialogResult.No) { Globals.notoriety += 1; } } } } }
and call it by putting random1(); in main how ever i get this error
Error 2 The name 'random1' does not exist in the current context C:\Users\ant\Documents\Visual Studio 2008\Projects\game\game\Form1.cs 119 17 game
-
just an update i got it working with the threadtime scheduling thanks for the tip however the 2nd bit of code still isnt working so i placed it in its own fucntion
/ rolling function
public class Globals6
{public static int dilemma = 0; public void random1() { Random dl = new Random(Environment.TickCount); Globals5.dilemma = dl.Next(1, 2); DialogResult res; if (Globals5.dilemma == 1) { res = MessageBox.Show("Ban same sex marriage", "Dilemma", MessageBoxButtons.YesNo); if (res == DialogResult.Yes) { Globals.notoriety -= 1; } else if (res == DialogResult.No) { Globals.notoriety += 1; } if (Globals5.dilemma == 2) { res = MessageBox.Show("Political protest, do you use essesive force?", "check", MessageBoxButtons.YesNo); if (res == DialogResult.Yes) { Globals.notoriety -= 1; } else if (res == DialogResult.No) { Globals.notoriety += 1; } } } } }
and call it by putting random1(); in main how ever i get this error
Error 2 The name 'random1' does not exist in the current context C:\Users\ant\Documents\Visual Studio 2008\Projects\game\game\Form1.cs 119 17 game
bump plz ive managed to get it working just not randomizing the second code it just plays message 1 constantly