你能帮我解决这里有两个或者一个问题吗,不确定。我正在尝试从几个private static string
中获取字符串和整数值,其中包含不同的进程,每个进程都有单个不同的类型输出,我希望它们作为函数顺序返回,以便进一步组合。
首先,我想说,我不知道,如何创建这样的函数,可以返回值来获得所需的结果,如果下面的方法是错误的,那么看看这个案例的一些有用的例子会很好,但是我想在这里做,给我两个问题:
如果我想要调用b1
然后再调用b2
并再次调用b1
,我希望从每个调用得到唯一的计算结果,但现在我得到了相同的结果,如果我使用随机数因为它显示如下,但我怀疑在这种情况下这个问题的原因是在private static string
内部错误使用随机,还需要弄清楚,如何正确使用它适用于这种情况。
结果如下所示:
23 23 23
但我认为这不仅是一个,同一个主要问题,因为如果我返回一些不同的计算结果,例如在私有静态字符串b1和b2中分别从不同列表的不同字符串集中选择,结果只是按顺序调用的顺序重复进程两次或多次返回相同的值。我不能肯定地说,但它看起来不像问题的原因,可以在上面。因为它看起来像这样:
77 34 77
或者为了更清楚,如果我通过计算形式字符串选择一些单词例如:“hello world,你好吗”并且在每个private static string
里面我做出不同的计算得到来自这个字符串的单个字,或来自不同的字母,我得到相同的结果或正确地说同一个过程的调用两次相同的结果,就像重复结果而不是单独调用它:
world you world
下面的示例仅显示带有随机数的情况,因为如果最后一个问题不同并且与使用错误的随机数没有关联,则必须采用一般方法,我应该以某种方式解决这个问题。
class Program
{
static void Main(string[] args)
{
string a1 = b1();
string a2 = b2();
string a3 = b1();
string comb = (a1 + a2 + a3);
Console.WriteLine(comb);
Console.ReadLine();
}
private static string b1()
{
Random random = new Random();
int ran1 = random.Next(1, 100);
return ran1;
}
private static string b2()
{
Random random = new Random();
int ran2 = random.Next(1, 100);
return ran2;
}
}
编辑1:
通过随机数或任何其他方式选择字符串返回的示例:
static void Main(string[] args)
{
string a1 = b1();
string a2 = b2();
string a3 = b1();
string comb = (a1 + a2 + a3);
Console.WriteLine(comb);
Console.ReadLine();
}
private static string b1()
{
Dictionary<int, string> mass1 = new Dictionary<int, string>()
{ { 1, "A" }, { 2, "B" }, { 3, "C" }};
Random random1 = new Random();
int rndCase2 = random1.Next(1, 4);
string key1;
mass1.TryGetValue(rndCase2, out key1);
return key1;
}
private static string b2()
{
Dictionary<int, string> mass2 = new Dictionary<int, string>()
{ { 1, "E" }, { 2, "F" }, { 3, "G" }};
Random random2 = new Random();
int rndCase2 = random2.Next(1, 4);
string key2;
mass2.TryGetValue(rndCase2, out key2);
return key2;
}
结果是:
AEA
编辑2 :(已解决)
class Program
{
private static Random random = new Random();
static void Main(string[] args)
{
string a1 = b1();
string a2 = b2();
string a3 = b1();
string comb = (a1 + a2 + a3);
Console.WriteLine(comb);
Console.ReadLine();
}
private static string b1()
{
Dictionary<int, string> mass1 = new Dictionary<int, string>()
{ { 1, "A" }, { 2, "B" }, { 3, "C" }};
return mass1[random.Next(1, 4)];
}
private static string b2()
{
Dictionary<int, string> mass2 = new Dictionary<int, string>()
{ { 1, "E" }, { 2, "F" }, { 3, "G" }};
return mass2[random.Next(1, 4)];
}
}
答案 0 :(得分:1)
仅使用一个Random
实例
class Program
{
static void Main(string[] args)
{
Random random = new Random();
string a1 = b1(random);
string a2 = b2(random);
string a3 = b1(random);
string comb = (a1 + a2 + a3);
Console.WriteLine(comb);
Console.ReadLine();
}
private static string b1(Random random)
{
int ran1 = random.Next(1, 100);
return ran1.ToString();
}
private static string b2(Random random)
{
int ran2 = random.Next(1, 100);
return ran2.ToString();
}
}
编辑1
同样适用于您的其他b1()
和b2()
方法:
private static string b1(Random random)
{
Dictionary<int, string> mass1 = new Dictionary<int, string>()
{ { 1, "A" }, { 2, "B" }, { 3, "C" }};
return mass1[random.Next(1, 4)];
}
private static string b2(Random random)
{
Dictionary<int, string> mass2 = new Dictionary<int, string>()
{ { 1, "E" }, { 2, "F" }, { 3, "G" }};
return mass2[random.Next(1, 4)];
}
Main()
与我之前的编辑保持一致
答案 1 :(得分:0)
由于Random
生成器在创建为new Random()
时从系统计时器初始化,因此您很可能有三个相同的所有三个调用中的生成器(具有相同的序列)。将Random
移出方法:
class Program {
// simplest, not thread safe
private static Random random = new Random();
static void Main(string[] args)
{
Random random = new Random();
string a1 = b1();
string a2 = b2();
string a3 = b1();
string comb = (a1 + a2 + a3); // <- please, notice "a3" instead of second "a1"
Console.WriteLine(comb);
Console.ReadLine();
}
private static string b1()
{
return random.Next(1, 100).ToString();
}
private static string b2()
{
return random.Next(1, 100).ToString();
}
}
在您编辑的代码中,请遵循相同的原则:不要创建 Random
个实例,而是使用static
一个:
private static string b1()
{
Dictionary<int, string> mass1 = new Dictionary<int, string>()
{ { 1, "A" }, { 2, "B" }, { 3, "C" }};
return mass1[random.Next(1, 4)];
}
private static string b2()
{
Dictionary<int, string> mass2 = new Dictionary<int, string>()
{ { 1, "E" }, { 2, "F" }, { 3, "G" }};
return mass2[random.Next(1, 4)];
}