[Computer ramble] pseudo-random number generation method

Source: Internet
Author: User
Tags natural logarithm

Hello, everybody. Talk a little bit about how to generate random numbers in the process of programming.

A Java pseudo-Random number generation method

Random numbers are widely used in the process of program design, especially in the field of practice environment simulation and testing, and we need to use random numbers in the process of programming. The random number in a computer is not a real random number, but a pseudo-random number. Is that the computer is modeled by a particular algorithm.

In the Java language, we can generate random numbers of interval [0,1] by the Math class 's Randomness method, and to generate a pseudo-random number between [a,a+b], you can pass the statement A + b * math.random (); To achieve. The method signature of the methods is: static double random ();

The math class is in the Java.lang package , and the Java.lang package is included by default, so you do not need to explicitly use the import java.lang.*; Statement. In the API documentation, we can know the inheritance structure of the class:

As you know, the math class is a direct subclass of the Obeject class, and the object class is a direct or indirect superclass of all other classes. The Math class declares a number of useful member variables and method members that are related to mathematical calculations, such as:

static double E; Base of natural logarithm

static double PI; As the name implies, Pi Pi

You can use them in your program to define variables instead of the utility const double.

Static double abs (double); To find the absolute value method

static double cos (double); cosine function, similar to this, there are sine sin method, tangent tan method, etc.

Static Double pow (double, double); Power function

Static double sqrt (double); Open Arithmetic square root

......

Two Pseudo-Random number generation method in C + +

There are no methods (functions) such as Math.random () in Java. The rand () function in the header file Stdlib.h or cstdlib is used. The function prototype is: int rand (); , generating a pseudo-random integer between 0 ~ Rand_max. Rand_max defined in Stdlib.h, the MinGW compiler on my machine is defined as: #define rand_max 0x7FFF

to generate a random number between (0,a), rand ()% (a+1) is available, ((double) rand ()/rand_max) * (b-a) + A, generating a random number on the interval [a, b].

However, this generated pseudo-random number is a fixed sequence, and when the rand () function is used again to produce a random number, it produces the same value as before. The improved method is to reset the random number seed (default is 1). Use the function void Srand (unsigned int) to set the random number seed, Change the sequence of pseudo-random numbers.

In order to be more random, the return value of the time function in the header file time.h is usually used as a random number seed, such as Srand ((unsigned) time (0));

The sample code is as follows:

1#include <iostream>2#include <cstdlib>3#include <ctime>4 5 using namespacestd;6 7 intMain () {8 9Srand ((unsigned) time (0));Ten  One       for(inti =0; I <Ten; i++) Acout<<Ten+rand ()% +<<' ';//random number between 10~30 -  -      return 0; the}

Thank you! Reprint Please specify the source, thank you for your cooperation!

[Computer ramble] pseudo-random number generation method

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.