Advertisement
Flavio1234

Criar coisas aleatoriamente

Sep 20th, 2020
766
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.63 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10.  
  11. namespace NumerosAleatorias
  12. {
  13.     public partial class Form1 : Form
  14.     {
  15.         public Form1()
  16.         {
  17.             InitializeComponent();
  18.         }
  19.  
  20.         private void button1_Click(object sender, EventArgs e)
  21.         {
  22.             lista.Items.Clear();
  23.  
  24.             for(int m= 0; m<10; m++){
  25.                 lista.Items.Add(CriarCodigo(m));
  26.             }
  27.  
  28.  
  29.  
  30.            
  31.  
  32.             //criar numeros aleatorios de 0 a 99
  33.             //Random gerador = new Random();
  34.  
  35.  
  36.             //for (int m = 0; m < 20; m++)
  37.             //{
  38.             //    int valor = gerador.Next(0,100);
  39.             //    lista.Items.Add(valor.ToString());
  40.  
  41.             //}
  42.         }
  43.  
  44.         private string CriarCodigo(int seed, int numeroCarateres = 30)
  45.         {
  46.             //gerar string com carateres aleatorios
  47.             StringBuilder str = new StringBuilder();
  48.  
  49.             string carateres = "ABCDEFGHIJKLMNOPKRSTUVWXYZabcdefghijklmnopkrstuvwxy";
  50.             Random r = new Random(seed);
  51.             int n = r.Next();
  52.  
  53.             for (int vezes = 0; vezes < numeroCarateres; vezes++)
  54.             {
  55.                 Random rnd = new Random(DateTime.Now.Millisecond + seed + n);
  56.  
  57.                 int x = rnd.Next(carateres.Length);
  58.                 str.Append(carateres[x]);
  59.                 n += 33;
  60.             }
  61.  
  62.  
  63.                 //Devolve str
  64.                 return str.ToString();
  65.         }
  66.     }
  67. }
  68.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement