Smart Contract

Random

A.e81193c424cfd3fb.Random

Deployed

2d ago
Feb 26, 2026, 01:49:14 PM UTC

Dependents

0 imports
1access(all) contract Random {
2	access(all) fun generateWithNumberSeed(seed: Number, amount: UInt8): [UFix64] {
3		let hash: [UInt8] = HashAlgorithm.SHA3_256.hash(seed.toBigEndianBytes())
4		return Random.generateWithBytesSeed(seed: hash, amount: amount)
5	}
6
7	access(all) fun generateWithBytesSeed(seed: [UInt8], amount: UInt8): [UFix64] {
8		let randoms: [UFix64] = []
9		var i: UInt8 = 0
10		while i < amount {
11			randoms.append(Random.generate(seed: seed))
12			seed[0] = seed[0] == 255 ? 0 : seed[0] + 1
13			i = i + 1
14		}
15		return randoms
16	}
17
18	access(all) fun generate(seed: [UInt8]): UFix64 {
19		let hash: [UInt8] = HashAlgorithm.KECCAK_256.hash(seed)
20		var value: UInt64 = 0
21
22		var i: Int = 0
23		while i < hash.length {
24				value = value + (UInt64(hash[i]))
25				value = value << 8
26				i = i + 1
27		}
28		value = value + UInt64(hash[0])
29		return UFix64(value % 100_000) / 100_000.0
30	}
31}
32