Click here to Skip to main content
11,930,222 members (39,125 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# C#4.0 string generator
I wonder how a program in C# that could produce food for a week but never repeated.
will generate input, meat, fish and dessert for each day of the week.

Monday Tuesday Wednesday Thursday Friday Saturday Sunday
bread ... ... ... ... ... ...
chicken ... ... ... ... ... ...
sardines ... ... ... ... ... ...
pineapple ... ... ... ... ... ...

Thank you already Wink | ;)
Posted 9-Aug-12 8:53am
Edited 9-Aug-12 9:04am
Volynsky Alex 10-Aug-12 4:22am
it mean you want generate random numbers (in range 1-7) with no repeat in c#?

How about:
public List uniqueRandomGenerator(int minVal, int maxVal)
Random rand = new Random();
SortedList uniqueList = new SortedList();
for (int i = minVal; i <= maxVal; i++)
uniqueList.Add(rand.Next(), i);

return uniqueList.Values.ToList();
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

All you need is a List(of Food) and a Random function... Besides you didnt tag the question on what languange you use....
Eating random food? No, thank you. :-)
Besides, OP's requirement "never repeated" makes no sense. Actually, both random menu and "never repeated" menu are both total absurd, just a useless exercise...
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

I'm going to expand on what Kenneth said, adding that as a food is used, remove it from the list, so it is never repeated.
po1725123 9-Aug-12 14:13pm
hi I already have all the food in the database, if I remove from the list i can not re-use then.
could show some examples how I can generate strings without repeating? I found nothing
wizardzz 9-Aug-12 14:22pm
Just one way: Load from Database to a 'working' list that they are selected and removed from, reload the list every week.

Or once selected, add to list. Then only select from database if not included in 'used' list.

There are many ways to do this.
Kenneth Haugland 9-Aug-12 14:28pm
LOL... Im sorry but the comment "I have all the food i need in the database" did me in.

If you allow the to be reused only not with the same dessert etc, we are talking heavy combinatorics and statistics, making the problem even wors to deal with. But I could easily predict how many variations that there would be, and the used combination could be stored in the database :-)

But Im not going to write that and give it over ....
This is not an expansion, but a solution (and random menu is not a solution). My 5.
However, the whole problem looks absurd to me (see my comment to Solution 1).

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web03 | 2.8.151126.1 | Last Updated 9 Aug 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100