Home > Back-end >  How to display all the numbers of my random c# form
How to display all the numbers of my random c# form

Time:08-30

I want to generate 500 random number between 1 to 200 and display all this number, but i have just one on the screen.

        private void cmdGenererNombres_Click(object sender, EventArgs e)
    {
        Random rand = new Random(500);
        int number = rand.Next(1, 201);

        FileStream fs = new FileStream("Binaire.bin", FileMode.Create, FileAccess.Write, FileShare.None);
        BinaryWriter bw = new BinaryWriter(fs);
        bw.Write(number);

        bw.Close();
        fs.Close();
    }

    private void cmdAfficherNombres_Click(object sender, EventArgs e)
    {
        FileStream fsSource = new FileStream("Binaire.bin", FileMode.Open, FileAccess.Read, FileShare.None);
        BinaryReader br = new BinaryReader(fsSource);

        MessageBox.Show(br.ReadByte().ToString());

        br.Close();
        fsSource.Close();
    }

CodePudding user response:

You can generate 500 random numbers and write into a file like this :

Random rand = new Random();
int[] rndArr = new int[500];

for (int i = 0; i < rndArr.Length; i  )
{
    int num = rand.Next(1, 200);
    rndArr[i] = num;
}

File.WriteAllLines("Binaire.bin", rndArr.Select(i => i.ToString()).ToArray());

And to show this into a messagebox from the file, you can do this :

MessageBox.Show(File.ReadAllText("Binaire.bin"));

CodePudding user response:

You could use a combination of Chunk and string.join to combine all the numbers into a single, large string.

var rand = new Random(42);
var rows = Enumerable.Range(0, 500)
    .Select(i => rand.Next(0, 201))
    .Chunk(50) // number of values per row
    .Select(r => string.Join(", ", r.ToString("000")); // Combine the values in each row. Use fixed width formatting

var block = string.Join(Environment.NewLine, rows); // combine rows into a block

MessageBox.Show(block);

Formatting the numbers as a block should help in keeping the text within the width and height of the screen.

An alternative would be to create your own UI with a scrollable textbox or something similar to show all the numbers.

CodePudding user response:

Try this for the first method:

int randomIntCount = 200;
Random rand = new Random();
FileStream fs = new FileStream("Binaire.bin", FileMode.Create, FileAccess.Write, FileShare.None);
BinaryWriter bw = new BinaryWriter(fs);

for (int i = 0; i < randomIntCount; i  )
{
   myRandomList[i]= i rand.Next(1, 201);
   bw.Write(number);
}

And for the cmdAfficherNombres_Click method, change it to this:

FileStream fsSource = new FileStream("Binaire.bin", FileMode.Open, FileAccess.Read, FileShare.None);
BinaryReader br = new BinaryReader(fsSource);
int randomIntCount = 200;

string result;
for (int i = 0; i < randomIntCount; i  )
{
    result  = " "   br.ReadInt32(); // separator here is a simple space
}
    
MessageBox.Show(result);
    
br.Close();
fsSource.Close();

Bon courage :)

  •  Tags:  
  • c#
  • Related