Click here to Skip to main content
16,020,622 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to create a random number 'n' amount of times depending on the number in a specific textbox - so if the textbox said 6, there would be six randomly generated numbers.
The code works, however, the same number is duplicated 'n' times instead of having 'n' different numbers and the first box in the item in the checkedlistbox is empty

can anyone help?
{
         Random generator = new Random();
         int r = generator.Next(100);
         string rnd = r.ToString("D3");
         int N = Convert.ToInt32(QuantityTxt.Text);

         for (int i = 0; i <= N; i++)
         {
         checkedListBox3.Text = BarcodeTxt.Text + "-" + rnd;
         checkedListBox3.Text.ToString().Split(',').ToList().ForEach(c => checkedListBox3.Items.Add(c.Trim()));
         }


What I have tried:

I've tried rearranging the numbers but nothing seems to work
Posted
Updated 24-Apr-18 23:09pm

You should move the random generation part into the loop if you want a new random number being generated at each iteration. Here you are generating the random number before the loop, so it's obvious why you get the same number. It is not duplicated, it is litterally the same number that you are using over and over.
 
Share this answer
 
Comments
CPallini 25-Apr-18 5:14am    
5.
Maciej Los 25-Apr-18 5:56am    
5ed!
phil.o is right: your code reuses the same variable each time.
C#
for (int i = 0; i <= N; i++)
    {
    checkedListBox3.Text = BarcodeTxt.Text + "-" + rnd; //<<<--- THIS VALUE NEVER CHANGES INSIDE THE LOOP.
    checkedListBox3.Text.ToString().Split(',').ToList().ForEach(c => checkedListBox3.Items.Add(c.Trim()));
    }
So the first thing to do is to move the random number generation code into the loop:
C#
for (int i = 0; i <= N; i++)
    {
    int r = generator.Next(100);
    string rnd = r.ToString("D3");
    checkedListBox3.Text = BarcodeTxt.Text + "-" + rnd; //<<<--- THIS VALUE NEVER CHANGES INSIDE THE LOOP.
    checkedListBox3.Text.ToString().Split(',').ToList().ForEach(c => checkedListBox3.Items.Add(c.Trim()));
    }
But ... that's some odd code!
You set the listbox text to a different value each time round the loop. That's OK - but it looks odd - it will leave the final value in the listbox.
You then get the value you just set, convert it to a string (which it is already), split it to an array on a character which is part of a string that doesn't change inside the loop, convert that array to a List - which you don't need to do, because Array has its own Foreach:
C#
string[] data = { "hello", "there" };
data.ToList().ForEach(s => Console.WriteLine(s));
Array.ForEach(data, s => Console.WriteLine(s));
The final two lines are equivelant but the latter doesn't mean creating new and unnecessary data structures.
And then you add them all to your list box.

So, depending on the content of Barcode.Text and how many commas it contains, you will add the same data several times, plus the "new" random number value.

Unless the Barcode is a specific type that allows non-numeric characters, that code will just add the barcode and the new random number each time. And most barcodes can't contain anything other than numeric data!

I'd suggest that you sit down and think carefully about exactly what you are trying to do here - that code looks a lot more complicated than I suspect it really needs to be.
 
Share this answer
 
Comments
CPallini 25-Apr-18 5:15am    
5.
Maciej Los 25-Apr-18 5:56am    
5ed!
If you would like to get 6 random numbers between 1 and 100, try this:

C#
//limit to 6 numbers
int limit = 6;
//Random.Next maxValue is exclusive upper bound of the random number returned, so we need to add 1
int cnt = 101;
//generate list if integers: 1 to 300 
List<int> numbers = Enumerable.Range(1,300).ToList();
//start randomize
Random rnd = new Random();
//get random numbers
var randomseq = numbers.Select(n=>n=rnd.Next(1,cnt)).Distinct().Take(limit).ToList();


For further details, please see: Random.Next Method (Int32, Int32) (System)[^]


Note: this method is not much efficient. Thanks to F-ES Sitecore[^ for valuable comment.
 
Share this answer
 
v2
Comments
CPallini 25-Apr-18 5:15am    
5.
Maciej Los 25-Apr-18 5:52am    
Thank you, Carlo.
F-ES Sitecore 25-Apr-18 5:27am    
That's an incredibly inefficient and slow way of generating random numbers. A simple for loop is much quicker, simpler, and easier to understand.
Maciej Los 25-Apr-18 5:55am    
Yeah, you're right, but OP didn't define criteria for algorithm. Provide your algorithm and take a 5 stars from me ;)
Cheers,
Maciej

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900