When the Generate button is pressed an array of doubles with a random length of 50 to 1000 values will be created. The array will be filled with random double values ( use rndGen.NextDouble( ) ) each with a range of 0.0 to 100.0. The contents of the array will be displayed in the textbox (shown in attached thumbnails) using two decimal places of accuracy.

my question is how to make random double values?
my code only giving me int.

Also i tried this
int iNum = rNumber.Next(0, 100);
double dNum = rNumber.NextDouble();
double dTotal = iNum + dNum;

but i don't know how to put in array

public partial class Form1 : Form
    {
        Random rNumber = new Random();
        
        public Form1()
        {
            InitializeComponent();

        }

        private void BTN_generate_Click(object sender, EventArgs e)
        {
            int ilength = rNumber.Next(50, 1000);
            int[] iArray = new int[ilength];
            for (int i = 0; i < iArray.Length; i++)
            {
                iArray[i] = rNumber.Next(101);
            }

            Console.WriteLine();
            foreach (int iValue in iArray)
            {
                textBox1.Text += iValue.ToString() + ", ";
            }

        }

Recommended Answers

All 2 Replies

Simply:

Random r = new Random();
        private void button1_Click(object sender, EventArgs e) //generate numbers by click:
        {
            int total = r.Next(50, 1001);
            List<double> numbers = new List<double>();
            for (int i = 0; i < total; i++)
                numbers.Add(Math.Round(0.0 + r.NextDouble() * 100.0, 2));
            textBox4.Text = String.Join(", ", numbers.Select(s => s.ToString()).ToArray());
        }

my question is how to make random double values?

From the problem statement : The array will be filled with random double values ( use rndGen.NextDouble( ) ) each with a range of 0.0 to 100.0.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.