There was a need to generate a pseudo-random number in binary representation (of three octets), for example, 10101111 00011100 00100001 or 00111100 01111000 01101101 (since ultimately the result will be represented by the type string, spaces are optional). I guess this is somehow implemented through the rand method, but this is only an assumption. I was never able to find the right direction in solving the problem in the official documentation. Please point me in the right direction.
- oneWell, generate, say, 3 pseudo-random from 0 to 255 and translate it into a binary system. Here you have 4 numbers in a binary representation of 8 numbers - Kir_Antipov
|
1 answer
Perhaps the simplest solution in this case is:
s = join(rand(0:1, 24)) First we get twenty-four numbers in the range from zero to one, after which join forms a string of the form 101010101100000111111100 from these numbers, a kind of pseudo-random binary number. If you still need to divide the string into octets:
join([s[i:i+7] for i in 1:8:24], " ") Here, in the cycle, substrings are extracted with a length of eight characters each, after which the substrings are combined into a string (separated by spaces) - I remind you that the index of the first element of the array in Julia is 1 . In principle, regular expressions can be used to divide a string, but this is a matter of taste.
|