随机数字(支持int64,支持double)

本文介绍了一个C#程序,用于生成指定范围内的随机数。程序能够处理整数和浮点数,并根据用户输入的不同情况采取不同的处理策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

namespace exam1
{

    class Program
    {

        static void Main(string[] args)
        {
            while (true)
            {
                Run run = new Run();
                run.Do();
                Console.WriteLine(是否要继续吗?(yn));
                string cin = Console.ReadLine();
                if (cin == y  cin == Y)
                {
                    continue;
                }
                else
                {
                    break;
                }

            }

        }

    }

    public class Run
    {

        const int Min32 = -2147483648;
        const int Max32 = 2147483647;
        public double max = 0;
        public void Do()
        {
            Console.WriteLine(输入最小值:);
            string sx = Console.ReadLine();
            Console.WriteLine(输入最大值:);
            string sy = Console.ReadLine();
            Console.WriteLine(要产生数据个数:);
            long number = long.Parse(Console.ReadLine());
            if (double.Parse(sx)  double.Parse(sy))  9 - 5
            {
                Console.WriteLine(最小值不能大于最大值!);
            }
            else if (double.Parse(sx) == double.Parse(sy))  10000-10000
            {
                Console.WriteLine(double.Parse(sx));
            }

            else
            {
                if (sx.IndexOf(.)  0 && sy.IndexOf(.)  0)   1000 - 100000
                {
                    long x = long.Parse(sx);
                    long y = long.Parse(sy);

                    for (int l = 0; l  number; l++)
                    {
                        Console.WriteLine(Rl(x, y));
                    }
                }
                else
                {
                    if (sx.IndexOf(.)  0 && sy.IndexOf(.)  0)   1.3333  - 2
                    {
                        long x = long.Parse(sx.Substring(0, sx.IndexOf(.)));
                        long y = long.Parse(sy);
                        for (int i = 0; i  number; i++)
                        {
                            string nn = (Rl(x, y) + Rd()).ToString();
                            Console.WriteLine(nn);
                        }
                    }

                    if (sx.IndexOf(.)  0 && sy.IndexOf(.)  0) 1 - 1.3333333  2 - 6.3333333
                    {
                        long x = long.Parse(sx) - 1;
                        long y = long.Parse(sy.Substring(0, sy.IndexOf(.)));
                        for (int i = 0; i  number; i++)
                        {
                            string nn = (Rl(x, y) + Rd()).ToString();
                            Console.WriteLine(nn);
                        }
                    }
                    if (sx.IndexOf(.)  0 && sy.IndexOf(.)  0)   2.3333333 - 3.3333333   5.66666 - 6.9999999
                    {
                        long x = long.Parse(sx.Substring(0, sx.IndexOf(.)));
                        long y = long.Parse(sy.Substring(0, sy.IndexOf(.)));
                        long dx = long.Parse(sx.Substring(sx.IndexOf(.) + 1, sx.Length - sx.IndexOf(.) - 1));
                        long dy = long.Parse(sy.Substring(sy.IndexOf(.) + 1, sy.Length - sy.IndexOf(.) - 1));

                        if (dx = dy)     0.66666 - 0.9999999    0.6666 - 0.6666
                        {
                            for (int i = 0; i  number; i++)
                            {
                                string nn = Rl(x, y).ToString() + . + Rl(dx, dy).ToString();
                                Console.WriteLine(nn);
                            }
                        }
                        else  5.99999 - 4.33333
                        {
                            for (int i = 0; i  number; i++)
                            {
                                string nn = (Rl(x, y) + Rd()).ToString();
                                Console.WriteLine(nn);
                            }
                        }

                    }

                }
            }
             Console.ReadKey();
        }

         summary
         int 32 随机数计算
         summary
         param name=x1最小值param
         param name=y2最大值param
         returnsreturns
        public static int R(int x1, int y2)
        {
            Thread.Sleep(100);
            Random random = new Random();
            int num = random.Next(x1, y2);
            return num;
        }

         summary
         int 32 随机数计算
         summary
         param name=xyparam
         returnsreturns
        public static int R(int xy)
        {
            Thread.Sleep(100);
            Random random = new Random();
            int num = random.Next(xy);
            return num;
        }
        public static double Rd()
        {
            Thread.Sleep(100);
            Random random = new Random();
            double num = random.NextDouble();

            return num;
        }

         summary
         int64 随机数计算
         summary
         param name=minparam
         param name=maxparam
         returnsreturns
        public static long Rl(long min, long max)
        {
            if (Min32 = min && max = Max32)
            {
                max = R(int.Parse(Convert.ToString(min)), int.Parse(Convert.ToString(max)));
            }
            else
            {
                long lon = max - min;
                long lon1 = lon - Max32;
                lon = lon1 + R(Max32);
                max = max - lon;
            }

            return max;
        }
    }
}

在Java中生成随机数字是一个常见的编程任务,通常可以通过`java.util.Random`类或者`java.lang.Math`类来实现。下面是两种主要的随机数字生成方法的简要介绍: 1. 使用`Random`类: `Random`类是Java中用于生成伪随机数的工具类。可以使用这个类的实例来生成不同类型的随机数,包括整型、浮点型等。创建一个`Random`对象后,可以调用其`nextInt()`, `nextDouble()`, `nextFloat()`等方法来获随机数。 示例代码: ```java import java.util.Random; public class RandomExample { public static void main(String[] args) { Random rand = new Random(); // 生成一个随机整数 int randomInt = rand.nextInt(); System.out.println("随机整数:" + randomInt); // 生成一个[0, 1)之间的随机浮点数 double randomDouble = rand.nextDouble(); System.out.println("随机浮点数:" + randomDouble); } } ``` 2. 使用`Math.random()`: `Math.random()`是一个静态方法,它返回一个大于或等于0.0且小于1.0的随机double值。如果需要生成其他范围内的随机数,可以通过对返回值进行适当的数学运算来实现。 示例代码: ```java public class MathRandomExample { public static void main(String[] args) { // 生成一个[0, 10)之间的随机浮点数 double randomDouble = Math.random() * 10; System.out.println("随机浮点数:" + randomDouble); // 如果需要生成随机整数,可以使用类型转换 int randomInt = (int)(Math.random() * 10); System.out.println("随机整数:" + randomInt); } } ``` 在使用`Math.random()`方法时,如果需要生成大于`Integer.MAX_VALUE`的随机数,那么这种方法就不再适用。在这种情况下,可以使用`Random`类来生成更大范围的随机数
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值