cwe-330-weak-prng

Replace insecure Java random generators with java.security.SecureRandom.

3|1|Updated Mar 6, 2026
One-click install
npx skills add https://github.com/DevelopersCoffee/java-cwe-security-skills --skill cwe-330-weak-prng
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: cwe-330-weak-prng
Source: https://github.com/DevelopersCoffee/java-cwe-security-skills/tree/main/cwe-330-weak-prng
Command: npx skills add https://github.com/DevelopersCoffee/java-cwe-security-skills --skill cwe-330-weak-prng

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

This Skill addresses vulnerabilities arising from the use of insufficient or predictable random number generators in Java applications, enhancing the security of cryptographic operations and session management.

Core Features & Use Cases

  • Vulnerability Remediation: Identifies and fixes the use of java.util.Random or Math.random() in security-sensitive contexts.
  • Secure Randomness: Replaces insecure random number generation with java.security.SecureRandom.
  • Use Case: When SAST tools flag CWE-330, this Skill can be applied to automatically update the code to use SecureRandom for generating session tokens, API keys, or other critical security values.

Quick Start

Use the cwe-330-weak-prng skill to replace insecure random number generation with SecureRandom in the provided Java code.

Frequently Asked Questions about cwe-330-weak-prng

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I fix CWE-330 weak random number generation in Java?

To fix CWE-330 weak random number generation in Java, replace insecure random number generators like java.util.Random or Math.random() with java.security.SecureRandom. This ensures sufficient entropy and avoids predictable seeding for generating security-sensitive values.

When should I use SecureRandom instead of Math.random in Java?

You must use SecureRandom instead of Math.random in Java when generating security-sensitive values like session tokens, CSRF tokens, or API keys. SecureRandom provides the cryptographic entropy required to prevent predictable random value vulnerabilities.

Why does using java.util.Random create a weak PRNG vulnerability?

Using java.util.Random creates a weak PRNG vulnerability because it relies on predictable seeding algorithms and lacks cryptographic entropy. This predictability allows attackers to guess generated values, compromising session tokens and API keys.

Can I automatically remediate predictable random values flagged by SAST tools?

Yes, you can automatically remediate predictable random values flagged by SAST tools by applying targeted updates. This process identifies insecure Java random generation contexts and directly replaces them with java.security.SecureRandom for enhanced cryptographic security.

What is the best way to generate secure session tokens in Java?

The best way to generate secure session tokens in Java is utilizing java.security.SecureRandom. It guarantees sufficient entropy during generation, ensuring your session tokens, CSRF tokens, and API keys remain cryptographically secure against prediction attacks.

Are there limitations when replacing java.util.Random with SecureRandom for API keys?

A key limitation when replacing java.util.Random with SecureRandom for API keys is potential performance overhead. SecureRandom relies on system entropy sources, which can slow down generation compared to standard PRNGs, requiring careful implementation in high-throughput environments.