当前位置:编程学习 > C#/ASP.NET >>

C#中怎么随机生成10个数,并升序输出

那位大哥帮我写哈 随机生成10个数,并按升序输出 --------------------编程问答--------------------

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

namespace rd
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] a = new int[10];
            Random r = new Random();
            for (int i = 0; i < 10; i++)
            {
                a[i] = r.Next();
            }
            Array.Sort(a);
            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine(a[i]);
            }
            Console.ReadLine();

        }
    }
}

--------------------编程问答-------------------- --------------------编程问答-------------------- --------------------编程问答-------------------- 一楼正解 --------------------编程问答-------------------- 我靠,都是现成的函数方法,LZ居然。。。。。。。。。
哎~~~~~~~~~~~~ --------------------编程问答-------------------- 顶1楼
LZ估计是新手。。 --------------------编程问答--------------------
引用 6 楼 myjavamosquito 的回复:
顶1楼
LZ估计是新手。。

铁定是新手... --------------------编程问答--------------------
引用 7 楼 yangjian0830 的回复:
引用 6 楼 myjavamosquito 的回复:
顶1楼
LZ估计是新手。。

铁定是新手...

+1 --------------------编程问答-------------------- +1
5楼不要嘲笑新手,大家都这么过来的 --------------------编程问答--------------------
引用 9 楼 think_fish1210 的回复:
+1
5楼不要嘲笑新手,大家都这么过来的


+1,
我也新手。 --------------------编程问答-------------------- 建议LZ有时间看看数据结构与算法
--------------------编程问答-------------------- 现在搞东西越来越简单。 --------------------编程问答--------------------
引用楼主 lp916854197 的回复:
那位大哥帮我写哈 随机生成10个数,并按升序输出

生成随机数和排序功能应该是程序员开始学软件开发时候考虑的问题 --------------------编程问答--------------------   Random r = new Random();
生成随机数

再有两都相比较的方法 判断 --------------------编程问答-------------------- 一楼回答正确 --------------------编程问答-------------------- 一楼回答正确 --------------------编程问答-------------------- 主要就是random。。。 --------------------编程问答-------------------- 回答的很不错 --------------------编程问答-------------------- mark 都是现成的函数 --------------------编程问答-------------------- --------------------编程问答-------------------- 我好久没有写了,也忘了,现在要捡起来不容易呀。 --------------------编程问答-------------------- 这好啊。c++也有 --------------------编程问答-------------------- --------------------编程问答-------------------- 大家也都是从新手走过来的............. --------------------编程问答-------------------- 一楼的正解! 不过,你只要理解思路,就可以自己写了!
主要运用随机函数生成10个数字,然后再排序输出! --------------------编程问答-------------------- --------------------编程问答-------------------- 降序的话,如何解决呢 ?
--------------------编程问答-------------------- 是不是没有现成的方法,得手动完成了? --------------------编程问答--------------------
引用 27 楼 kingdom_0 的回复:
降序的话,如何解决呢 ?

降序Array有一个倒置的方法..排序之后再倒置就行了 --------------------编程问答-------------------- --------------------编程问答-------------------- 一楼的答案正解,但是万一要是楼主要求10个数必须完全不同呢?
一楼的还是有一定几率出现重复数字的。

有空嘲笑新手,还不如稍微把它完善一下。 --------------------编程问答-------------------- 无重复的10个随机数:
 public static Random r = new Random();
        public static void Main(string[] args)
        {
            string[] str=new string[10];
            Random r=new Random();
            for (int i = 0; i < 10; i++)
            {
                str[i] = r.Next().ToString();
            }
            DistinctData(str);
            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine(str[i]);
            }
                Console.ReadKey();
        }
        public static void DistinctData(string[] str)
        {
            string[] temp;
            temp = str.Distinct().ToArray();
            if (temp.Length == 10)
            {
                return;
            }
            if (temp.Length < 10)
            {
                for (int i = temp.Length; i < 10; i++)
                {
                    temp[i] = r.Next().ToString();
                  
                }
            }
            DistinctData(temp);
        }
    欢迎拍砖。 --------------------编程问答-------------------- --------------------编程问答-------------------- --------------------编程问答-------------------- 生成十个完全不同的随机数并排序
http://blog.csdn.net/hzleexia/archive/2011/04/08/6309884.aspx --------------------编程问答-------------------- --------------------编程问答-------------------- 多看看书啦  --------------------编程问答-------------------- 无重复的10个随机数:
 public static Random r = new Random();
  public static void Main(string[] args)
  {
  string[] str=new string[10];
  Random r=new Random();
  for (int i = 0; i < 10; i++)
  {
  str[i] = r.Next().ToString();
  }
  DistinctData(str);
  for (int i = 0; i < 10; i++)
  {
  Console.WriteLine(str[i]);
  }
  Console.ReadKey();
  }
  public static void DistinctData(string[] str)
  {
  string[] temp;
  temp = str.Distinct().ToArray();
  if (temp.Length == 10)
  {
  return;
  }
  if (temp.Length < 10)
  {
  for (int i = temp.Length; i < 10; i++)
  {
  temp[i] = r.Next().ToString();
    
  }
  }
  DistinctData(temp);
  }
  欢迎拍砖。



我这里有个方法。大家瞅瞅:
   生成无重复的十个数字
         private void GetRandom() 
        {
            int[] myArray = new int[10];
            Random rand = new Random();
            for (int i = 0; i < 10; i++) 
            {
                int num = rand.Next(1, 100);
                myArray[i] = num;
                for (int j = 0; j <= i; j++) 
                {
                    if (myArray[j] == num) 
                    {
                        i--;
                        break;
                    }
                }
            }
        } --------------------编程问答--------------------
引用 13 楼 hooverhuang 的回复:
引用楼主 lp916854197 的回复:
那位大哥帮我写哈 随机生成10个数,并按升序输出

生成随机数和排序功能应该是程序员开始学软件开发时候考虑的问题

是吗?难道所有的程序都有随机数的需求吗 --------------------编程问答-------------------- Array.Sort(a);
降序怎么办?》 --------------------编程问答-------------------- 我还说简单的来取点分 我想发帖问个问题,诶 --------------------编程问答--------------------
楼主刚入门吧~~

学过随机函数和排序函数都会的~~~

觉得不应该给楼主代码,告诉他方法就行了。

不然楼主太懒,我们反而害了他~~~
补充:.NET技术 ,  其他语言
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,