如何生成唯一的随机十进制数 [英] How can i generate random decimal numbers in unique

查看:90
本文介绍了如何生成唯一的随机十进制数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好如何生成随机十进制数



Hi How can i generate random decimal numbers

public static string createRandomNumbers(int Length)
       {
           

           string _allowedChars = "1234560789";
           Random randNum = new Random();
           char[] chars = new char[Length];
           int allowedCharCount = _allowedChars.Length;

           for (int i = 0; i < Length; i++)
           {
               chars[i] = _allowedChars[(int)((_allowedChars.Length) * randNum.NextDouble())];
           }

           return new string(chars);

       }







生成随机数的代码





喜欢这个我要生成随机十进制数



如何获得随机小数数字



数据类型十进制(8,2),即点数2位后的8位数

喜欢我将传递两个长度..为此能怎么写函数? ...




This code for generating random numbers


like This i want to generate random decimal numbers

how to get random decimal numbers

data type decimal(8,2) that is 8 digit after point 2 digit
like dis i will pass two length..for this how can able to write function? ...

推荐答案

我会采取一些不同的方法:

I would take a bit different approach:
public static string randdec(Random r, int l1, int l2)
{
  int L1= (int)Math.Pow(10, l1-1);
  int L2 = (int)Math.Pow(10, l2-1);
  int n1 = r.Next(L1, 10*L1);
  int n2 = r.Next(L2, 10*L2);
  return n1.ToString() + "." + n2.ToString();
}





您可以这样使用



that you may use this way

public static void Main()
{
  Random r = new Random();
  for (int n = 0; n < 10; n++)
    Console.WriteLine(randdec(r,8,2));
}


这篇关于如何生成唯一的随机十进制数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆