Code for random function

MATLABrandom function of various uses and examples

In MATLAB numerical operations and the laws of exploration, often need to use the random function to generate some special matrices, the following we will be introduced through the example of the use of random function.

helprandom we first look at the helprandom. In the main window type helprandom. enter, we can see its related help, we take R=random(NAME,A) as an example, in the format of this function, generated by the type of NAME distribution, A is the type of response parameter, according to the type of NAME, A is different. NAME type, the dimension of A is different, and its specific meaning is also different. The following figure shows the various types of NAME specific, there are exponential distribution, normal distribution, F distribution and so on.

We will introduce the exponential distribution as an example, the type of exponential distribution is ‘exp’, which requires one parameter. Usage 1: random(‘exp’,A) in the main window of MATLAB type, R = random(‘exp’,3), enter, we can see the following results: generated is an exponential distribution parameter is 3 random values.

Usage 2: random(‘exp’,A,a) in the main window of MATLAB enter R=random(‘exp’,3,6), enter, we can see that the generated is a random square matrix of order 6 in accordance with the exponential distribution.

Usage 2: random(‘exp’,A,a,b) in the MATLAB main window enter R=random(‘exp’,3,6,2), enter, we can see the generated is the 6th order conforms to the exponential distribution of 6 rows and 2 columns of the random square matrix.

js generate random number of random function random example

js generate random number can use JavaScript

Math.random() built-in function, there is a good example below, you can try to operate

JavaScript

Math.random() Built-in function

random function return value

Returns a pseudo-random number between 0 and 1, may be 0, but always less than 1, [0,1)

random function example

document.write(Math.random());

//Returns a random number in the range 10-20

document.write(Math.random()*(20-10)+10);

//Returns the formula for a random number in the specified range (between m-n)

document.write(Math.random()*(n-m));

document.write(Math. (n-m)+m);

Based on time, it is also possible to generate random numbers

The code is as follows:

var

now=new

Date();

var

number

=

now. getSeconds();

// This produces an integer from 0 to 59 based on the current time.

var

now=new

Date();

var

number

=

now.getSeconds()%43;

// This produces an integer from 0 to 42 based on the current time.

Use import to import the random module. Use the random() function under this module to generate a random floating point number between 0 and 1?

The following is the code to generate a random floating point number between 0 and 1 using the random module, and use print() to output the result:

Executing the above code will print a random floating point number between 0 and 1 on the screen, and the sample output is as follows:

Each time you execute it the result may be different because the number generated is random.

关于C++中的random函数

#include<iostream>

#include<ctime>

usingnamespacestd;

intmain()

{

srand((unsigned)time(NULL));

cout<<rand()%6+1<<endl;

return0;

}