代码改变世界

解决C#用随机数产生的坐标在对角线上

2010-09-02 13:11  蜗小牛  阅读(466)  评论(0编辑  收藏  举报

 /// <summary>
        /// 初始化坐标
        /// </summary>
        /// <returns></returns>
        private Point InitPoint()
        {
            int x, y;
            long tick = DateTime.Now.Ticks;
            pointX = new Random(GetRandomSeed());
            pointY = new Random(GetRandomSeed());
            x = pointX.Next(0,this.panel1.Width-50);
            y = pointY.Next(0,this.panel1.Height-50);
            p = new Point(x, y);
            return p;
        }
        /// <summary>
        /// 随机生成种子数(摘抄)
        /// </summary>
        /// <returns></returns>
        static int GetRandomSeed()
        {
            byte[] bytes = new byte[4];
            RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
            rng.GetBytes(bytes);
            return BitConverter.ToInt32(bytes, 0);
        }