Actions

  Print Article
  BookMark Article

Categories    Category List

Arts & Humanities
  Books & Authors
  Dancing
  Genealogy
  History
  Performing Arts
  Philosophy
  Poetry
  Theater & Acting
  Visual Arts
Beauty & Style
  Fashion & Accessories
  Hair
  Makeup
  Skin & Body
Business & Finance
  Advertising & Marketing
  Careers & Employment
  Corporations
  Credit
  Insurance
  Investing
  Personal Finance
  Renting & Real Estate
  Small Business
  Taxes
Cars & Transportation
  Aircraft
  Boats & Boating
  Car
  Insurance & Registration
  Maintenance & Repairs
  Motorcycles
  Rail
Computers & Internet
  Computer Networking
  Hardware
  Internet
  Programming & Design
  Security
  Software
Consumer Electronics
  Camcorders
  Cameras
  Cell Phones & Plans
  Games & Gear
  Home Theater
  Music & Music Players
  PDAs & Handhelds
  TVs
Dining Out
Education & Reference
  Financial Aid
  Higher Education
  Preschool
  Primary & Secondary Education
  Special Education
  Studying Abroad
  Teaching
Entertainment & Music
  Celebrities
  Comics & Animation
  Horoscopes
  Jokes & Riddles
  Magazines
  Movies
  Music
  Polls & Surveys
  Radio
  Television
Environment
  Alternative Fuel Vehicles
  Conservation
  Global Warming
  Green Living
Family & Relationships
  Family
  Friends
  Marriage & Divorce
  Singles & Dating
  Weddings
Food & Drink
  Beer, Wine & Spirits
  Cooking & Recipes
  Ethnic Cuisine
  Vegetarian & Vegan
Games & Recreation
  Amusement Parks
  Board Games
  Card Games
  Gambling
  Hobbies & Crafts
  Toys
  Video & Online Games
Health
  Alternative Medicine
  Dental
  Diet & Fitness
  Diseases & Conditions
  General Health Care
  Men's Health
  Mental Health
  Optical
  Women's Health
Home & Garden
  Cleaning & Laundry
  Decorating & Remodeling
  Do It Yourself (DIY)
  Garden & Landscape
  Maintenance & Repairs
Local Businesses
News & Events
  Current Events
  Media & Journalism
Pets
Politics & Government
  Civic Participation
  Elections
  Embassies & Consulates
  Government
  Immigration
  International Organizations
  Law & Ethics
  Military
  Politics
Pregnancy & Parenting
  Adolescent
  Adoption
  Baby Names
  Newborn & Baby
  Parenting
  Pregnancy
  Toddler & Preschooler
  Trying to Conceive
Science & Mathematics
  Agriculture
  Astronomy & Space
  Biology
  Botany
  Chemistry
  Earth Sciences & Geology
  Engineering
  Geography
  Mathematics
  Medicine
  Physics
  Weather
  Zoology
Social Science
  Anthropology
  Dream Interpretation
  Economics
  Gender Studies
  Psychology
  Sociology
Society & Culture
  Community Service
  Cultures & Groups
  Etiquette
  Holidays
  Languages
  Mythology & Folklore
  Religion & Spirituality
  Royalty
Sports
Travel

Online Now    Online Now

Author Login    Author Login

Welcome Guest! Please login or create an account.

Username:

Password:



Navigation    Navigation

ADS    Featured Author

ad

ADSDisclosure

Please help me with this C# code.?

Author :

Submitted : 2018-06-15 15:05:09    Popularity:     

Tags: code  

So I have included a photo of what must be done. I have successfully got the code to do random numbers however they ask to use a loop function to stop numbers from being repeated. I do not know how/what to use in the loop function, anyone care to help me

Answers:

int intValue;
Random MyRand = new Random();

int[] numbers = new int[5];
for (int i = 0; i < numbers.Length; i++) {
intValue = MyRand.Next(1, 50);
if (numbers.Contains(intValue)) {
--i; //// Duplicated dont count
} else {
numbers[i] = intValue;
}
}

txtRand1.Text = numbers[0].ToString();

Here is a console program that you can look at:

public class Test {
static Random MyRand = new Random ();
static int rand () {
return MyRand.Next (1, 10);
}

public static void Main () {
int [] arr = new int [6];
for (int i=0; i<6; i++) {
arr [i] = rand ();
Console.WriteLine (arr [i]);
for (int j=0; j<i; j++) {
Console.WriteLine ("Checking " + arr [j]);
if (arr [i] == arr [j]) {
Console.WriteLine ("Duplicate ");
i--;
}
}
}
}
}

Something seems wrong with the instructions your teacher provided for the rand () function so I didn't follow it exactly.

And I changed the range to be smaller to make the likelyhood of duplicates higher, so that you could see it finding duplicates. You still might have to run it a few times to see it exhibit the behavior.

https://ideone.com/GuHySR

I mean, even what you have done, you haven't followed the instructions. Where's the Rand function the instructions tell you to create.

Use a Do..while/until loop to not include a duplicate number. And use a List<t> instead of Array. You have more functionality using List<t>. List<t> is the improved array.

The hard part of this assignment is to avoid duplicates. If you keep your values in separate variables, you'll end up with some very long statements.

The simplest way to get a unique random number is to simply discard duplicates as you generate them. The non-loop code might look like:

int n1, n2, n3, n4, n5;

n1 = MyRand(1, 50);

do { n2 = MyRand(1, 50); } while (n2 == n1);

do { n3 = MyRand(1,50); } while (n3 == n1 || n3 == n2);

... and so on. The idea in that last statement is to repeatedly generate new random values for n3 until you get one that different from both n1 and n2. So, you will repeat the loop if n1 is equal to either n1 or n2.

You need to run that loop body at least once to get a value to test, which is why I used a do-while loop instead of a while. Note that n1 can be generated without needing a loop.

What Chris (aka "?" these days, but I suspect that's a website glitch) said about using an array is a good thing to consider. If you wanted to write a code that would work for different lotto systems, with 6 or more number choices or a different range of numbers, then "hard coding" for just 5 choices and a range of 1-49 will make changing the code both necessary and more difficult that it needs to be.

Keep this in mind for later. For now, "hard coding" for 5 numbers isn't too bad. If you know how to make symbolic constants, using something like LOTTO_MAX+1 might make it clear just what that 50 means in your program.

Another way to get unique values from a small set is to make an array of all possible values and "shuffle" the contents randomly. The classic way to do this is called the Knuth-Fisher-Yates shuffle, but the idea is obvious enough that many programmers have reinvented it. To shuffle an array of n items, with index values 0 through n-1:

for (i=0; i<n-1; ++n) // for each index except the last
{ swap the contents of array[i] with array[MyRand.next(i, n)] }

Since that only reorders the elements, you don't have to recreate the array for each new shuffle.

For generating just 5 unique elements, you only need to run the outer loop 5 times and you'll get 5 unique random numbers from the original array in the first 5 location of the array using only five random number generations.

Using a loop here first requires using an array to store the random numbers.

That means that when you get the random number #2, you have to check it against random #1 and if it is equal, you have to try and get another #2.
When you get #3, you have to check it against #1 and #2; and so on.

Structurally, it would be a lot better to have the call to the random fonction inside a loop that runs until all 6 values are established, with an internal loop that runs from the first to the last one already established (i.e. n-1), to check against previous values, with a BREAK command to request a replacement for a value that happens to repeat.

And you would have better luck posting such questions in the "computer and internet", "programming and design" or "software" category.

People in the Politics section aren't smart enough to understand what you're talking about



Good
Back Homepage
Back


Article Source:
www.Aphotolog.Com

Answer Questions