Hi,
I used Random class for get random numbers, but it bring me a same number every time.
Here is my code:
Random
rnd = new Random();newArr[j++] = (
char)(rnd.Next(65, 122));any ideas?
thanks.
Hi,
I used Random class for get random numbers, but it bring me a same number every time.
Here is my code:
Random
rnd = new Random();newArr[j++] = (
char)(rnd.Next(65, 122));thanks.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
Vimal KandasamyRank #2,151 · 661 reputationPosted Mar 19, 2009, 12:08 AM EDT
for me it shows different chars.
i execute your code as
Random rnd = new Random();
Console.WriteLine( (char)(rnd.Next(65, 122)));
Console.WriteLine((char)(rnd.Next(65, 122)));
output is
1st time
R
h
2nd
Y
g
3rd
Q
j
problem may be in your code.
Steve6 reputationPosted Mar 19, 2009, 12:15 AM EDT
Are you just trying to get a random number? or are you trying to throw a random number into an array...?
just random number..
Random myrandom = new Random();
int number = myrandom.Next(min value, max value);
for an array...
Random myrandom = new Random();
int number = myrandom.Next(min value, max value);
int[] myarray = new int[number];
sara shats15 reputationPosted Mar 19, 2009, 2:42 AM EDT
I can't find any problem or mistake in my code, it can be an Application error?
or maybe missing any 'using'
(it's asp.net Application whis c# code)
Vimal KandasamyRank #2,151 · 661 reputationPosted Mar 19, 2009, 2:55 AM EDT
it may help you to fix the problem
sara shats15 reputationPosted Mar 19, 2009, 3:06 AM EDT
I am trying to put 2 random chars between every char in my Array
for (int i = 0, j = 0; i < originalArr.Length; i++)
{
newArr[j++] =
originalArr[i] ;for (int k = 0; k < 2; k++)
{
Random rnd = new Random();
newArr[j++] = (char)(rnd.Next(65, 122));
}
}
The original string was "sara"
the result is "sHHaHHrHHaHH"
I saw in the MSDN help a method: Math.Randomize, (in c# methods), but I can't use it.
What can I do?
thanks a lot!
Vimal KandasamyRank #2,151 · 661 reputationPosted Mar 19, 2009, 4:38 AM EDT
put Random object creation before the for loop like below.
Random rnd = new Random();
for (int i = 0, j = 0; i < originalArr.Length; i++)
{
newArr[j++] = originalArr[i];
for (int k = 0; k < 2; k++)
{
newArr[j++] = (char)(rnd.Next(65, 122));
}
}
Niradhip ChakrabortyRank #429 · 5.8K reputationPosted Mar 19, 2009, 4:56 AM EDT
try this:
protected void Page_Load(object sender, EventArgs e)
{
int intreturnValue = RandomNumber(100000, 999999);
}
private int RandomNumber(int min, int max)
{
Random random = new Random();
return random.Next(min, max);
}
sara shats15 reputationPosted Jun 18, 2009, 8:40 AM EDT