The timestamp of C # is used in flash actionscript,

Source: Internet
Author: User
Tags define function random seed

The timestamp of C # is used in flash actionscript,

Currently, we are working on a project to transmit timestamps to flash in bytes,

If the error is returned, the following code can be used:

C #

            DateTime centuryBegin = new DateTime(1970, 1, 1,0,0,0);            DateTime currentDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute,DateTime.Now.Second);            long elapsedTicks = currentDate.Ticks - centuryBegin.Ticks;

14071475100000000 output

Flash actionscript

Var d: Date = new Date (0); trace (d); d. setMilliseconds (1407147510000); // 14071475100000000 is passed over to 10 ^ 9 in nanoseconds, and here is millisecond trace (d );


 

Output
Mon Aug 4 18:18:30 GMT + 0800 2014

Note that in C #, the nanoseconds are 10 ^ 4 times more.

While as is 1407147510000 in milliseconds


C language rand () function

Rand and srand usage
First, we need to have a general opinion on rand & srand: srand initializes random seeds, and rand generates random numbers. The following describes in detail.

Rand (Random Number Generation)
Header file: # include <stdlib. h>

Define function: int rand (void)

Function Description:
Because the internal implementation of rand is made by the linear same remainder method, it is not a real random number, but because its cycle is very long, so it can be considered as random within a certain range, rand () returns a random value ranging from 0 to RAND_MAX. Before calling this function to generate a random number, you must use srand () to set the random number seed. If no random number seed is set, rand () will automatically set the random number seed to 1. Rand () generates false random numbers, which are the same during each execution. To be different, initialize it with different values. The initialized function is srand ().

Return Value:
Returns a random integer between 0 and RAND_MAX. The range of RAND_MAX is at least 32767 (int), that is, double byte (16 digits ). If unsigned int is used, the dual-byte value is 65535, and the four-byte value is an integer range of 4294967295.
0 ~ RAND_MAX the probability of each number being selected is the same.

Example:
/* Generate a random value ranging from 1 to 10. In this example, no random seed is set. For the complete random number generation, see
Srand ()*/
# Include <stdlib. h>
Main ()
{
Int I, j;
For (I = 0; I <10; I ++)
{
J = 1 + (int) (10.0 * rand ()/(RAND_MAX + 1.0 ));
Printf ("% d", j );
}
}
Run:

9 4 8 8 10 2 4 8 3 6
9 4 8 8 10 2 4 8 3 6 // re-execution still produces the same random number

Srand (set Random Seed)
Header file: # include <stdlib. h>

Define the function: void srand (unsigned int seed );

Function Description:
Srand () is used to set the random number seed when rand () generates a random number. The seed parameter must be an integer. Generally, the return value of geypid () or time (0) can be used as seed. If the same value is set for each seed, the random values generated by rand () are the same each time.

Example
/* Generate a random number ranging from 1 to 10. This example and execution result can be referenced with rand */
# Include <time. h>
# Include <stdlib. h>
Main ()
{
Int I, j;
Srand (int) time (0 ));
For (I = 0; I <10; I ++)
{
J = 1 + (int) (10.0 * rand ()/(RAND_MAX + 1.0 ));
Printf ("% d", j );
}
}
Execution: Compare with the rand example
5 8 8 8 10 2 10 8 9 9
2 9 7 4 10 3 2 10 8 7
Or:
Use "int x = rand () % 100;" to generate a random number between 0 and 100. This method is not or can be used. A better method is j = (int) (n * rand ()/(RAND_MAX + 1.0) generates a random number between 0 and n.
Int main (void)
{
Int I;
Time_t t;
Srand (unsigned) time (& t ));
Printf ("Ten r... the remaining full text>

C language rand () function

Rand and srand usage
First, we need to have a general opinion on rand & srand: srand initializes random seeds, and rand generates random numbers. The following describes in detail.

Rand (Random Number Generation)
Header file: # include <stdlib. h>

Define function: int rand (void)

Function Description:
Because the internal implementation of rand is made by the linear same remainder method, it is not a real random number, but because its cycle is very long, so it can be considered as random within a certain range, rand () returns a random value ranging from 0 to RAND_MAX. Before calling this function to generate a random number, you must use srand () to set the random number seed. If no random number seed is set, rand () will automatically set the random number seed to 1. Rand () generates false random numbers, which are the same during each execution. To be different, initialize it with different values. The initialized function is srand ().

Return Value:
Returns a random integer between 0 and RAND_MAX. The range of RAND_MAX is at least 32767 (int), that is, double byte (16 digits ). If unsigned int is used, the dual-byte value is 65535, and the four-byte value is an integer range of 4294967295.
0 ~ RAND_MAX the probability of each number being selected is the same.

Example:
/* Generate a random value ranging from 1 to 10. In this example, no random seed is set. For the complete random number generation, see
Srand ()*/
# Include <stdlib. h>
Main ()
{
Int I, j;
For (I = 0; I <10; I ++)
{
J = 1 + (int) (10.0 * rand ()/(RAND_MAX + 1.0 ));
Printf ("% d", j );
}
}
Run:

9 4 8 8 10 2 4 8 3 6
9 4 8 8 10 2 4 8 3 6 // re-execution still produces the same random number

Srand (set Random Seed)
Header file: # include <stdlib. h>

Define the function: void srand (unsigned int seed );

Function Description:
Srand () is used to set the random number seed when rand () generates a random number. The seed parameter must be an integer. Generally, the return value of geypid () or time (0) can be used as seed. If the same value is set for each seed, the random values generated by rand () are the same each time.

Example
/* Generate a random number ranging from 1 to 10. This example and execution result can be referenced with rand */
# Include <time. h>
# Include <stdlib. h>
Main ()
{
Int I, j;
Srand (int) time (0 ));
For (I = 0; I <10; I ++)
{
J = 1 + (int) (10.0 * rand ()/(RAND_MAX + 1.0 ));
Printf ("% d", j );
}
}
Execution: Compare with the rand example
5 8 8 8 10 2 10 8 9 9
2 9 7 4 10 3 2 10 8 7
Or:
Use "int x = rand () % 100;" to generate a random number between 0 and 100. This method is not or can be used. A better method is j = (int) (n * rand ()/(RAND_MAX + 1.0) generates a random number between 0 and n.
Int main (void)
{
Int I;
Time_t t;
Srand (unsigned) time (& t ));
Printf ("Ten r... the remaining full text>

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.