Advertisement
elena1234

AdvertisementMessage- how to use Random (return random int index)

Feb 10th, 2021 (edited)
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.93 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3.  
  4. namespace AdvertisementMessage
  5. {
  6.     public class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             var phrases = new string[] {
  11.                 "Excellent product",
  12.                 "Such a great product.",
  13.                 "I always use that product.",
  14.                 "Best product of itscategory.",
  15.                 "Exceptional product.",
  16.                 "I can’t live without this product."};
  17.  
  18.             var events = new string[] {
  19.                 "Now I feel good.",
  20.                 "I have succeeded with this product.",
  21.                 "Makes miracles. I am happy of the results!",
  22.                 "I cannot believe but now I feel awesome.",
  23.                 "Try it yourself, I am very satisfied.",
  24.                 "I feel great"};
  25.  
  26.             var authors = new string[]
  27.             {"Diana", "Petya", "Stella", "Elena", "Katya", "Iva", "Annie", "Eva"};
  28.  
  29.             var cities = new string[]
  30.             {"Burgas", "Sofia", "Plovdiv", "Varna", "Ruse"};
  31.  
  32.  
  33.             int numberOfMessages = int.Parse(Console.ReadLine());
  34.             Random generator = new Random(); // Random is out the loop NB!
  35.             for (int i = 0; i < numberOfMessages; i++)
  36.             {
  37.                 var sb = new StringBuilder();
  38.                 int indexOfPhrase = generator.Next(phrases.Length);
  39.                 sb.Append(phrases[indexOfPhrase]);
  40.                 sb.Append(" ");
  41.                 int indexOfEvent = generator.Next(events.Length);
  42.                 sb.Append(events[indexOfEvent]);
  43.                 sb.Append(" ");
  44.                 int indexOfAuthor = generator.Next(authors.Length);
  45.                 sb.Append(authors[indexOfAuthor]);
  46.                 sb.Append(" - ");
  47.                 int indexOfCity = generator.Next(cities.Length);
  48.                 sb.Append(cities[indexOfCity]);
  49.                 Console.WriteLine(sb);
  50.             }
  51.         }
  52.     }
  53. }
  54.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement