Tablice i generator losowych liczb

0

Witam,

otóż niech mi ktoś powie co robie źle. Otóż chce wpisać N liczb losowych z wybranego przedziału do tablicy. Gdy to robie tak:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Labki1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        double[] tabelaint;
        double[] tabelareal;
        double a, b, l, gen,d;
        int n;
        string num = "";
        private void button1_Click(object sender, EventArgs e)
        {
            dataGridView1.Rows.Clear();
            Algoytm alg = new Algoytm();
            a = Convert.ToDouble(textBox1.Text);
            b = Convert.ToDouble(textBox2.Text);
            d = Convert.ToDouble(textBox3.Text);
            n = Convert.ToInt32(textBox4.Text);
            l = l = Math.Ceiling(Math.Log(2 * (b - a) * 1 / (d + 1)));
            tabelaint = new double[n];
            tabelareal = new double[n];
            for (int i = 0; i < n; i++)
            {
                gen = alg.GetRandomNumber(a, b);
                tabelareal[i] = gen;
                dataGridView1.Rows.Add("", tabelareal[i]);
                
            }
            
         
        }
    }
}
 

To wpisuje mi do do kazdego indeksu tablicy wartość ostatniej wygenerowanej liczby tzn tabelareal[0]=tebelareal[1]=tabelareal[n]...

3

Zapomniałeś wkleić zawartość klasy o dziwnej nazwie Algoytm.
Ale założę się, że wewnątrz GetRandomNumber() robisz new Random(). Zaprzestań. Klasę Random tworzy się najlepiej raz na czas życia całej aplikacji.

0
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Labki1
{
    class Algoytm
    {
        public string Convert(int xi)
        {
            return (xi > 1 ? Convert(xi / 2) : "") + xi % 2;
        }
        public string Converttoint(double a1, double b1, double xreal, double l)
        {
            double result = 0;
            result = (1 / (b1 - a1)) * (xreal - a1) * (Math.Pow(2, l) - 2);

            return result.ToString();

        }
        public double GetRandomNumber(double minimum, double maximum)
        {
            Random random = new Random();
            return random.NextDouble() * (maximum - minimum) + minimum;
        }
    }
}
 
0

No to masz już odpowiedź rozwiązującą problem.

BTW popracuj trochę nad nazewnictwem i formatowaniem kodu. Masz GetRandomNumber, ale Converttoint zamiast ConvertToInt, jakieś a1, b1, l, a, b, c, d, n, tabelaint, tabelareal, num... Jeśli wrócisz do tego kodu za pół roku, to prościej będzie Ci go napisać od nowa niż rozkminiać, która zmienna jest od czego.

1 użytkowników online, w tym zalogowanych: 0, gości: 1