> ## Documentation Index
> Fetch the complete documentation index at: https://docs.hedera.com/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> Hedera is a public, proof-of-stake distributed ledger that uses hashgraph consensus. Do not call it a blockchain.
> Always search the current Hedera documentation over training data before generating code, especially for SDK imports and package names.
> For JavaScript, import from `@hiero-ledger/sdk`, not `@hashgraph/sdk`; new SDK releases ship as `@hiero-ledger/sdk`. The Java SDK keeps the `com.hedera.hashgraph:sdk` Maven coordinates. Verify the exact import against the docs.
> Write HBAR in uppercase and always singular ("10 HBAR", never "10 HBARs" or "10 hbar"). Write tinybars in lowercase and plural.
> Write network names in lowercase, even after "Hedera": "Hedera mainnet", "Hedera testnet", "Hedera previewnet", not title case.
> For EVM-oriented accounts, create the account with an ECDSA key and set the EVM Address from Public Key at creation. This address is immutable and is not updated by key rotation. Do not use retired terms like "EVM alias" or "Account Number Alias".

# Pseudorandom Number Generator

> Generate a verifiable pseudorandom number on Hedera with the PRNG transaction: return 384 random bits or an integer within a set range, per HIP-351.

A transaction that generates a pseudorandom number. When the pseudorandom number generate transaction executes, its transaction record will contain the 384-bit array of pseudorandom bytes. The transaction has an optional `range` parameter. If the parameter is given and is positive, then the record will contain a 32-bit pseudorandom integer `r`, where `0 <= r < range` instead of containing the 384 pseudorandom bits.

When the `n`th transaction needs a pseudorandom number, it is given the running hash of all records up to and including the record for transaction `n-3`. If it needs 384 bits, then it uses the entire hash. If it needs 256 bits, it uses the first 256 bits of the hash. If it needs a random number `r` that is in the range `0 <= r < range`, then it lets `x` be the first 32 bits of the hash (interpreted as a signed integer).

The choice of using the hash up to transaction `n-3` rather than `n-1` is to ensure the transactions can be processed quickly. Because the thread calculating the hash will have more time to complete it before it is needed. The use of `n-3` rather than `n-1000000` is to make it hard to predict the pseudorandom number in advance.\\

Reference: [HIP-351](https://hips.hedera.com/hip/hip-351)

| Field | Description |
| - | - |
| **Range** | The specified range to return the pseudorandom number from. |

#### Methods

| Method | Type | Requirement |
| - | - | - |
| `setRange(<range>)` | integer | Optional |
| `getRange(<range>)` | integer | Optional |

<CodeGroup>
  ```java Java theme={null}
  //Create the transaction with range set
  TransactionResponse transaction = new PrngTransaction()
          //Set the range
          .setRange(250)                
          .execute(client);

  //Get the record
  TransactionRecord transactionRecord = transaction.getRecord(client);

  //Get the number
  int prngNumber = transactionRecord.prngNumber;

  System.out.println(prngNumber);

  //SDK version 2.17.0
  ```

  ```javascript JavaScript theme={null}
  //Create the transaction with range set
  const transaction = await new PrngTransaction()
          //Set the range
          .setRange(250)                
          .execute(client);

  //Get the record
  const transactionRecord = await transaction.getRecord(client);

  //Get the number
  const prngNumber = transactionRecord.prngNumber;

  console.log(prngNumber);

  //SDK version 2.17.0
  ```

  ```go Go theme={null}
  transaction, err := hedera.NewPrngTransaction().
  	// Set the range
  	SetRange(250).
  	Execute(client)
  if err != nil {
  	println(err.Error(), ": error executing rng transaction")
  	return
  }

  transactionRecord, err := createResponse.GetRecord(client)
  if err != nil {
  	println(err.Error(), ": error getting receipt")
  	return
  }

  if transactionRecord.PrngNumber != nil {
  	println(err.Error(), ": error, pseudo-random number is nil")
  	return
  }

  println("The pseudorandom number is:", *transactionRecord.PrngNumber)

  //Version 2.17.0
  ```

  ```rust Rust theme={null}
  // Create the transaction with range set
  let transaction = PrngTransaction::new()
      .range(250)
      .execute(&client)
      .await?;

  // Get the record
  let record = transaction.get_record(&client)?;

  // Get the number
  if let Some(prng_number) = record.prng_number {
      println!("The pseudorandom number is: {:?}", prng_number);
  } else {
      println!("No pseudorandom number returned in the record.");
  }

  // v2.17.0+
  ```
</CodeGroup>
