| |

VerySource

 Forgot password?
 Register
Search
View: 1037|Reply: 7

How to randomly generate integers with a specified number of digits?

[Copy link]

1

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-1-17 15:20:02
| Show all posts |Read mode
How to randomly generate integers with a specified number of digits?
Reply

Use magic Report

2

Threads

20

Posts

12.00

Credits

Newbie

Rank: 1

Credits
12.00

 China

Post time: 2020-1-23 00:45:01
| Show all posts
What does it mean? If you want to generate random integers ranging from N to N + M-1, just let
result = rand ()% M + N
If you want to generate random two digits,
result = rand ()% 100
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-1-23 10:09:01
| Show all posts
Passing by
learning
Reply

Use magic Report

0

Threads

9

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-1-27 09:18:01
| Show all posts
srand (time (NULL));
iRand1 = rand ()% 100;
iRand2 = rand ()% 1000;
Reply

Use magic Report

1

Threads

14

Posts

13.00

Credits

Newbie

Rank: 1

Credits
13.00

 China

Post time: 2020-1-27 11:09:01
| Show all posts
Right upstairs3区尤娜
Reply

Use magic Report

0

Threads

57

Posts

27.00

Credits

Newbie

Rank: 1

Credits
27.00

 China

Post time: 2020-1-28 15:36:02
| Show all posts
1 digit is (0, 10)
2 digits (10,100)
...
N digits (100 ... 0 (N-1 0), 100 ... 0 (N))
Reply

Use magic Report

0

Threads

4

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-1-28 20:54:01
| Show all posts
rand
Reply

Use magic Report

2

Threads

19

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

Post time: 2020-1-29 23:36:01
| Show all posts
// Get floating-point random numbers between 0-1
DOUBLE GetRand0 ()
{
     srand (timeGetTime ());
     DOUBLE d = (DOUBLE) GetRand (0,1000) /1000.0;
return d;
}

// Get integer random number
// Parameter: minN The minimum number to generate a random number
// maxN: maximum number of random numbers

INT GetRand (INT minN, INT maxN)
{
     srand (timeGetTime ());
     INT r = minN + rand ()% (maxN + 1);
     return r;
}
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list