954,518 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

foreach statements with lists

Hey guys. Im try to write a program with 3 major things which are two lists and a random.

what i want to do is say whatever values are in those lists then produce so many random value if you get what i mean.

so say list one value is 3, and list two is 4 than it should produce 12 random numbers.

I hope this doesnt sound too complicated. The reason I want to do this is shorten the code as much as possible.

Thanks

ogsirus
Newbie Poster
16 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
 

Hmmm, sounds like a homework assignment:)
It is fairly simple to accomplish though. I'm assuming your two lists have the values preset. So you just need to find the selected item of each list, convert to integer and multiply them.
Once you have that value a loop that includes that value as the maximum can be done that calculates a new random number each iteration of the loop.
Have a go and post up some code when you get stuck.

hericles
Practically a Posting Shark
823 posts since Nov 2007
Reputation Points: 136
Solved Threads: 167
 

Nope not a homework assignment. Im not even in school lol. Just a bit self studying.

I'll have a bit a go at it now.

ogsirus
Newbie Poster
16 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
 

What is the actual problem?
So far, it's just multiplication to get a result, right?

using System;
using System.Collections.Generic;

namespace DW_405186_CS_CON
{
   class Program
   {
      static void Main(string[] args)
      {
         List<int> lst_intSeed1 = new List<int>() { 5, 1, 3, 2, 4 };
         List<int> lst_intSeed2 = new List<int>() { 4, 1, 7 };

         lst_intSeed1.ForEach(i =>
            lst_intSeed2.ForEach(j =>
               {
                  Console.WriteLine("{0} * {1} = {2}", i, j, (i * j));
               }));
      }
   }
}


...and then the "overkill"

using System;
using System.Collections.Generic;
using System.Linq;

namespace DW_405186_CS_CON
{
   class Program
   {
      static void Main(string[] args)
      {
         List<int> lst_intSeed1 = new List<int>() { 5, 1, 3, 2, 4 };
         List<int> lst_intSeed2 = new List<int>() { 4, 1, 7 };

         lst_intSeed1.ForEach(i =>
            lst_intSeed2.ForEach(j =>
               {
                  Console.Write("{0} * {1} = {2}:\n [ ", i, j, (i * j));
                  
                  Random rnd = new Random((int)DateTime.Now.Ticks % int.MaxValue);
                  
                  Enumerable.Range(1, (i * j)).ToList().ForEach(e => Console.Write("{0} ", rnd.Next(1, 100)));
                  
                  Console.WriteLine(']');
               }));
      }
   }
}
thines01
Postaholic
Team Colleague
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
 

My lists are doubles not ints and i have not tried to use an array. maybe i should.

ogsirus
Newbie Poster
16 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
 
My lists are doubles not ints


I'm confused. Just change them to doublesand i have not tried to use an array. maybe i should.
What do you mean? What's wrong with a list?

It will only get more complicated with an array (or two).

thines01
Postaholic
Team Colleague
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: