forked from BigEggStudy/LeetCode-CS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0470-ImplementRand10UsingRand7.cs
More file actions
38 lines (32 loc) · 909 Bytes
/
0470-ImplementRand10UsingRand7.cs
File metadata and controls
38 lines (32 loc) · 909 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//-----------------------------------------------------------------------------
// Runtime: 228ms
// Memory Usage: 29.3 MB
// Link: https://leetcode.com/submissions/detail/387689354/
//-----------------------------------------------------------------------------
using System;
namespace LeetCode
{
/**
* The Rand7() API is already defined in the parent class SolBase.
* public int Rand7();
* @return a random integer in the range 1 to 7
*/
public class _0470_ImplementRand10UsingRand7 : Rand7Base
{
public int Rand10()
{
int r = 40;
while (r >= 40)
r = (Rand7() - 1) * 7 + Rand7() - 1;
return r % 10 + 1;
}
}
public class Rand7Base
{
private readonly Random random = new Random();
public int Rand7()
{
return random.Next(7) + 1;
}
}
}