3 Replies - 461 Views - Last Post: 09 November 2013 - 10:40 AM Rate Topic: -----

#1 vdroy97  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 24-December 12

Java Math.random() Equivalent function in c#

Posted 15 January 2013 - 03:41 PM

Any one describe Java Math.random() Equivalent function in C# generates random floating point numbers return like this
0.45028185518458486
Is This A Good Question/Topic? 0
  • +

Replies To: Java Math.random() Equivalent function in c#

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4307
  • View blog
  • Posts: 12,085
  • Joined: 18-April 07

Re: Java Math.random() Equivalent function in c#

Posted 15 January 2013 - 03:46 PM

You can use the Random class and its method "NextDouble" which will give you a number between 0.0 and 1.0.

Hope that helps. :)

This post has been edited by Martyr2: 15 January 2013 - 03:47 PM

Was This Post Helpful? 0
  • +
  • -

#3 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: Java Math.random() Equivalent function in c#

Posted 15 January 2013 - 04:11 PM

The Java Math.random() method actually uses a static Random object in the Math class, and just calls nextDouble() on it when you call Math.random().

So it really is pretty much exactly the same :)
Was This Post Helpful? 0
  • +
  • -

#4 Egypt_Coder  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 1
  • Joined: 09-November 13

Re: Java Math.random() Equivalent function in c#

Posted 09 November 2013 - 10:40 AM

Hello sorry cz its 2 late 2 answer
this what u want . this code generate random 5 letters from a 2 z
for 10 times . i use random class in c# that like math.random in java.

CODE BY Me

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

namespace ConsoleApplication1
{
    class Program
    {
       
        static void Main(string[] args)
        {
           string tt= "";


           Random r = new Random();
           for (int m = 1; m <= 10; m++)
           {
               for (int i = 1; i <= 5; i++)
               {
                   double x = (double)r.NextDouble();
                   int rand1 = (int)(26 * x) + 97;
                   char c = (char)rand1;
                   tt += c;

               }
               Console.WriteLine(tt);
               tt = "";
              
           }
             
          
        }
    }
}


This post has been edited by Skydiver: 09 November 2013 - 02:05 PM
Reason for edit:: Put code in code tags. Learn to do this yourself.

Was This Post Helpful? -1
  • +
  • -

Page 1 of 1