Reputation: 1609
To apply in a blockchain application, I needed to generate random 64-digit hexadecimal numbers in R.
I thought that due to the capacity of computers, obtaining such an 64-digit hexadecimal number at once is rather cumbersome, perhaps impossible.
So, I thought that I should produce random hexadecimal numbers with rather low digits, and bring (concatenate) them together to obtain random 64-digit hexadecimal number.
I am near solution:
library(fBasics)
.dec.to.hex(abs(ceiling(rnorm(1) * 1e6)))
produces random hexadecimal numbers. The problem is that in some of the instances, I get 6-digit hexadecimal number, in some instances I get 7-digit hexadecimal number. Hence, fixing this became priority first.
Any idea?
Upvotes: 2
Views: 2287
Reputation: 31452
You can simply sample each digit and paste them together.
set.seed(123)
paste0(sample(c(0:9, LETTERS[1:6]), 64, T), collapse = '')
## [1] "4C6EF08E87F7A91E305FEBAFAB8942FEBC07C353266522374D07C1832CE5A164"
Upvotes: 9
Reputation: 1609
Max argument of .dec.to.hex()
is .dec.to.hex(2^30.99999....9)
.
So, the question reduces to 2^30.99999=2147468763
is what power of 10?
2147468763 = 2.147468763e9
1e9 < 2.147468763e9. Hence 9th power. But, rnorm(1)
may produce ">5
". For safety, use 8th power (.dec.to.hex(abs(ceiling(rnorm(1) * 1e8)))
is 7 or 8 hexa-digits. 10*7 >= 64
).
library(fBasics)
strtrim(paste(sapply(1:10, function(i) .dec.to.hex(abs(ceiling(rnorm(1) * 1e8)))), collapse=""), 64)
# 0397601803C22E220509810703BDE2300460EA80322F000CF50ABD0226F27009
10 iterations instead of 11; hence, with a little bit less operations!
nchar(strtrim(paste(sapply(1:10, function(i) .dec.to.hex(abs(ceiling(rnorm(1) * 1e8)))), collapse=""), 64))
# 64
Upvotes: 0
Reputation: 1609
library(fBasics)
strtrim(paste(sapply(1:11, function(i) .dec.to.hex(abs(ceiling(rnorm(1) * 1e6)))), collapse=""), 64)
# 08FBFA019B4930E2AF707AFEE08A0F90D765E05757607609B0691190FC54E012
Let's check:
nchar(strtrim(paste(sapply(1:11, function(i) .dec.to.hex(abs(ceiling(rnorm(1) * 1e6)))), collapse=""), 64)) # 64
Upvotes: -1