none
How to choose random from collection(Dictionarystring,int) RRS feed

  • Question

  •                 var proxyz = new Dictionary<string, int>();
                    proxyz.Add("138.68.161.14", 3128);
                    proxyz.Add("138.68.165.154", 8080);
                    proxyz.Add("139.59.169.246", 3128);
                    proxyz.Add("169.48.152.174", 3128);

    client.Proxy = new WebProxy("ip", port); I randomly select the IP and port mapped to the IP from dictionary and use it as WebProxy

    The IP is a string. The Port is Int32

    Any idea how can this be done  Thank you.


    • Edited by HisKingdom Saturday, March 7, 2020 3:15 AM
    Saturday, March 7, 2020 2:38 AM

Answers

  • Hello,

    A simpler idea in regards to getting both ip and port is via a class and extension.

    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;
    
    namespace WindowsFormsApp1
    {
        public partial class Form1 : Form
        {
            private readonly List<Item> _itemsList = new List<Item>()
            {
                new Item() {Ip = "138.68.161.14", Port = 3128},
                new Item() {Ip = "138.68.165.154", Port = 8080},
                new Item() {Ip = "139.59.169.246", Port = 3128},
                new Item() {Ip = "169.48.152.174", Port = 3128}
            };
    
            public Form1()
            {
                InitializeComponent();
            }
            private void button1_Click(object sender, EventArgs e)
            {
                var item = _itemsList.RandomElement();
                var ip = item.Ip;
                var port = item.Port;
            }
        }
    
        #region Place in it's own class file
    
        public class Item
        {
            public string Ip { get; set; }
            public int Port { get; set; }
            public override string ToString() => $"{Ip},{Port} ";
    
        }
    
        #endregion
    
        #region Place in it's own class file
    
        public static class CollectionExtension
        {
            private static Random rng = new Random();
    
            public static T RandomElement<T>(this IList<T> list)
            {
                return list[rng.Next(list.Count)];
            }
    
            public static T RandomElement<T>(this T[] array)
            {
                return array[rng.Next(array.Length)];
            }
        }
    
        #endregion
    }
    

     


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Saturday, March 7, 2020 4:26 AM
    Moderator

All replies

  • Hello,

    A simpler idea in regards to getting both ip and port is via a class and extension.

    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;
    
    namespace WindowsFormsApp1
    {
        public partial class Form1 : Form
        {
            private readonly List<Item> _itemsList = new List<Item>()
            {
                new Item() {Ip = "138.68.161.14", Port = 3128},
                new Item() {Ip = "138.68.165.154", Port = 8080},
                new Item() {Ip = "139.59.169.246", Port = 3128},
                new Item() {Ip = "169.48.152.174", Port = 3128}
            };
    
            public Form1()
            {
                InitializeComponent();
            }
            private void button1_Click(object sender, EventArgs e)
            {
                var item = _itemsList.RandomElement();
                var ip = item.Ip;
                var port = item.Port;
            }
        }
    
        #region Place in it's own class file
    
        public class Item
        {
            public string Ip { get; set; }
            public int Port { get; set; }
            public override string ToString() => $"{Ip},{Port} ";
    
        }
    
        #endregion
    
        #region Place in it's own class file
    
        public static class CollectionExtension
        {
            private static Random rng = new Random();
    
            public static T RandomElement<T>(this IList<T> list)
            {
                return list[rng.Next(list.Count)];
            }
    
            public static T RandomElement<T>(this T[] array)
            {
                return array[rng.Next(array.Length)];
            }
        }
    
        #endregion
    }
    

     


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Saturday, March 7, 2020 4:26 AM
    Moderator
  • In case of dictionaries, try this fragment too:

       var kvp = proxyz.Skip( rnd.Next( proxyz.Count ) ).First( );

       string ip = kvp.Key;

       int port = kvp.Value;

    where rnd is a Random object.

    • Proposed as answer by CoolDadTxModerator Saturday, March 7, 2020 5:01 PM
    • Marked as answer by HisKingdom Saturday, March 7, 2020 7:40 PM
    • Unmarked as answer by HisKingdom Saturday, March 7, 2020 7:43 PM
    Saturday, March 7, 2020 9:53 AM
  • Thank this works just perfectly.
    Saturday, March 7, 2020 7:43 PM