1/1页1 跳转到查看:894
发新话题 回复该主题

奇怪的 Random

奇怪的 Random

刚才有位同学 问了 我个问题,是关于random的,很是奇怪,我也很不解。希望明白的同学指点一下。
代码很简单,就是输出几个随机数,程序如下:

        protected void Button1_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < 5; i++)
            {
                Response.Write(RandPass() + "\n");
            }
        }
        private string RandPass()
        {
            Random rand = new Random();
            string password = "";
            string[] passArray = new string[] { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "*", "%", "$", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
            for (int i = 0; i < 10; i++)
            {
                int number = rand.Next(passArray.Length);
                password += passArray[number];
            }
            return password;
        }


这段代码执行后,输出的是 5组 相同的随机字符串,更奇怪的是,当给代码设置断点后,单步执行时,输出的竟是5组不同的随机数了。
单步执行 与 正常运行 输出的结果竟然不同!
个人认为,这可能是random的问题,将代码改成如下,就正常了。

  Random rand = new Random();
        protected void Button1_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < 5; i++)
            {
                Response.Write(RandPass() + "\n");
            }
        }
        private string RandPass()
        {
            string password = "";
            string[] passArray = new string[] { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "*", "%", "$", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
            for (int i = 0; i < 10; i++)
            {
                int number = rand.Next(passArray.Length);
                password += passArray[number];
            }
            return password;
        }


这其中的原因,我也不太明白,希望同学们指点。
http://www.Aspx1.Com
请帮忙宣传Aspx1 , Aspx1是ASP.NET学习者的家园 , 适宜长期居住.

TOP

 

回复 1# aspx1 的帖子

这个应该和引用有关,我个人认为:
因为Random rand=new Random();
这里new起到的作用是从托管堆中分派指定类型所需数量的字节来作为存储其对象的内存空间;
每个对象实例都有两个附加成员,一个为指向类型方法表的指针,第2个为SyncBlockIndex;完成new是,返回一个指向新创建对象的引用.

在第二个方法中:
只有一个指向,也就一个引用.也就是在内存中是同一地址.每次随机改变出来的值就不同了.
在第一种方法中:
每次都创建一个引用.也就是在内存中每次都是不同的地址:对应的应该是都是第一次随机出来的值,同时指向了5个引用.

也有可能和编译有关系.确实有问题,我也试过了,只能解释这么多了.
本帖被评分 1 次

TOP

 

回复 1# aspx1 的帖子

using System;
using System.Collections.Generic;
using System.Text;

namespace ExamRandom
{
    class Program
    {
       
        static void Main(string[] args)
        {
            Random random = new Random();
         
            for (int i = 0; i < 5;i++ )
            {
                string str = postStr(random);
                Console.WriteLine(str);

            }
        }

        public static string postStr(Random random)
        {
           

            string password = "";
            string[] strList = new string[] { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "*", "%", "$", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };

            for (int i = 0; i < 10;i++ )
            {
                int number = random.Next(strList.Length);

                password+=strList[number];

            }

            return password;
        }
    }
}

TOP

 

谢谢楼上的解释,不过还不是很明白。楼下继续
http://www.Aspx1.Com
请帮忙宣传Aspx1 , Aspx1是ASP.NET学习者的家园 , 适宜长期居住.

TOP

 

在cnblogs获得了答案,共享给大家:

Random rand = new Random();
是以当前时间做为种子的。如果不在调试模式,5次循环中的时间几乎一样,所以生成的随机数也是一样。如果在单步状态下,5次的时间不一样,所以随机数会不同
http://www.Aspx1.Com
请帮忙宣传Aspx1 , Aspx1是ASP.NET学习者的家园 , 适宜长期居住.

TOP

 
1/1页1 跳转到
发表新主题 回复该主题