Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.

Problem sa nizom.

[es] :: .NET :: Problem sa nizom.

[ Pregleda: 1770 | Odgovora: 2 ] > FB > Twit

Postavi temu Odgovori

Autor

Pretraga teme: Traži
Markiranje Štampanje RSS

nervozica
Novi Beograd

Član broj: 18671
Poruke: 77
195.252.81.*



Profil

icon Problem sa nizom.31.01.2005. u 15:30 - pre 233 meseci

Kada zelim random brojeve za radiobutton1, koji vraca niz od 7 brojeva, nema problema. za radiobutton2 (vraca 3 niza po 7 brojeva) sva tri niza su ista.
Npr:
5, 32, 16, 27, 33, 17, 23
5, 32, 16, 27, 33, 17, 23
5, 32, 16, 27, 33, 17, 23

ali kad prodjem kroz debuger dobijem razlicite nizove
Npr:
19, 10, 12, 20, 27, 32, 17
9, 29, 2, 26, 18, 35, 36
24, 14, 4, 17, 35, 3, 18

Postoji li neki valjani razlog za ovaj slucaj?

Evo i koda u prilog:

Code:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace Loto
{
    /// <summary>
    /// Summary description for Form1.
    /// </summary>
    public class Loto : System.Windows.Forms.Form
    {
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.ListBox listBox1;
        private System.Windows.Forms.RadioButton radioButton1;
        private System.Windows.Forms.RadioButton radioButton2;
        private System.Windows.Forms.RadioButton radioButton3;
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public Loto()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

            //
            // TODO: Add any constructor code after InitializeComponent call
            //

        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null) 
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.button1 = new System.Windows.Forms.Button();
            this.listBox1 = new System.Windows.Forms.ListBox();
            this.radioButton1 = new System.Windows.Forms.RadioButton();
            this.radioButton2 = new System.Windows.Forms.RadioButton();
            this.radioButton3 = new System.Windows.Forms.RadioButton();
            this.SuspendLayout();
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(168, 120);
            this.button1.Name = "button1";
            this.button1.TabIndex = 0;
            this.button1.Text = "button1";
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // listBox1
            // 
            this.listBox1.Location = new System.Drawing.Point(8, 8);
            this.listBox1.Name = "listBox1";
            this.listBox1.Size = new System.Drawing.Size(144, 134);
            this.listBox1.TabIndex = 6;
            // 
            // radioButton1
            // 
            this.radioButton1.Location = new System.Drawing.Point(160, 8);
            this.radioButton1.Name = "radioButton1";
            this.radioButton1.Size = new System.Drawing.Size(88, 24);
            this.radioButton1.TabIndex = 0;
            this.radioButton1.Text = "radioButton1";
            // 
            // radioButton2
            // 
            this.radioButton2.Location = new System.Drawing.Point(160, 40);
            this.radioButton2.Name = "radioButton2";
            this.radioButton2.Size = new System.Drawing.Size(88, 24);
            this.radioButton2.TabIndex = 1;
            this.radioButton2.Text = "radioButton2";
            // 
            // radioButton3
            // 
            this.radioButton3.Location = new System.Drawing.Point(160, 72);
            this.radioButton3.Name = "radioButton3";
            this.radioButton3.Size = new System.Drawing.Size(88, 24);
            this.radioButton3.TabIndex = 2;
            this.radioButton3.Text = "radioButton3";
            // 
            // Loto
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(256, 150);
            this.Controls.Add(this.listBox1);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.radioButton3);
            this.Controls.Add(this.radioButton2);
            this.Controls.Add(this.radioButton1);
            this.Name = "Loto";
            this.Text = "Form1";
            this.ResumeLayout(false);

        }
        #endregion

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main() 
        {
            Application.Run(new Loto());
        }

        public string GetRndLotoNumber()
        {
            bool firstTime    = true;
            bool IsExist = false;
            int tmpNum = 0;
            string line = string.Empty;

            int[] arr = new int[7];
            Random rnd = new Random();

            for(int i=0; i<arr.Length; i++)
            {
                tmpNum = rnd.Next(1, 39);

                if(firstTime)
                {
                    arr.SetValue(tmpNum, 0); // prvi broj
                    firstTime = false;
                    continue;
                }

                for(int x=0; x<arr.Length; x++)
                {
                    if(tmpNum == (int)arr[x]) // da li postoji broj
                    {
                        IsExist = true; 
                        i--;
                    }
                }

                if(!IsExist)
                    arr.SetValue(tmpNum, i);
                
                IsExist = false;
            }

            for(int i=0; i<arr.Length; i++)
                line += (i == 6)? arr[i].ToString() : arr[i].ToString() +", ";

            return line;
        }

        private void button1_Click(object sender, System.EventArgs e)
        {
            listBox1.Items.Clear();

            if(radioButton1.Checked)
                listBox1.Items.Add(GetRndLotoNumber());
            else if(radioButton2.Checked)
                for(int i=0; i<3; i++)
                   listBox1.Items.Add(GetRndLotoNumber());
            else if(radioButton3.Checked)
                for(int i=0; i<10; i++)
                                   listBox1.Items.Add(GetRndLotoNumber());
        }
    }
}

Djankam se, kuzish.
 
Odgovor na temu

jablan

Član broj: 8286
Poruke: 4541



+711 Profil

icon Re: Problem sa nizom.31.01.2005. u 16:04 - pre 233 meseci
Pogledaj help za Random() konstruktor. Ako suviše brzo uzastopno inicijalizuješ random generator, vraćaće ti iste sekvence.

Predlažem ti da Random klasu instanciraš samo jednom i držiš je kao atribut klase.
 
Odgovor na temu

nervozica
Novi Beograd

Član broj: 18671
Poruke: 77
195.252.81.*



Profil

icon Re: Problem sa nizom.01.02.2005. u 07:55 - pre 233 meseci
Thanks. To je bio problem.
Djankam se, kuzish.
 
Odgovor na temu

[es] :: .NET :: Problem sa nizom.

[ Pregleda: 1770 | Odgovora: 2 ] > FB > Twit

Postavi temu Odgovori

Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.