Skip to content

Commit 37b156b

Browse files
committed
Fix: Add random.RandIPv4, random.RandIPv4Private, random.RandIPv4Loopback, random.RandIPv6 Functions
Signed-off-by: vr-varad <varadgupta21@gmail.com>
1 parent f7a52e1 commit 37b156b

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

random/random.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,44 @@ func RandIPv4() net.IP {
152152
)
153153
}
154154

155+
// RandIPv4Private Generates a random private IPv4 address.
156+
func RandIPv4Private() net.IP {
157+
n, _ := rand.Int(rand.Reader, big.NewInt(3))
158+
switch n.Int64() {
159+
case 0:
160+
return net.IPv4(
161+
10,
162+
byte(RandIntRange(1, 256)),
163+
byte(RandIntRange(1, 256)),
164+
byte(RandIntRange(1, 256)),
165+
)
166+
case 1:
167+
return net.IPv4(
168+
172,
169+
byte(RandIntRange(16, 32)),
170+
byte(RandIntRange(1, 256)),
171+
byte(RandIntRange(1, 256)),
172+
)
173+
default:
174+
return net.IPv4(
175+
192,
176+
168,
177+
byte(RandIntRange(1, 256)),
178+
byte(RandIntRange(1, 256)),
179+
)
180+
}
181+
}
182+
183+
// RandomIPv4Loopback generates IPv4 Loopback address
184+
func RandIPv4Loopback() net.IP {
185+
return net.IPv4(
186+
127,
187+
0,
188+
0,
189+
1,
190+
)
191+
}
192+
155193
// RandIPv6 generates a random IPv6 address.
156194
func RandIPv6() net.IP {
157195
ip := make(net.IP, net.IPv6len)

0 commit comments

Comments
 (0)