Click here to Skip to main content
15,884,237 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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);


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? ...
Updated 17-Apr-16 9:15am

1 solution

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++)
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900