Bookmark this: tool.io/random/radix-value/source-code
Random Base Number Generator Source Code
Browse the implementation files for Random Base Number Generator.tool.ts typescript
import {
calculateRandomRadixValue,
type RandomRadixValue,
type SupportedRadix
} from '@tool.io/tools/random/radix-value';
import type { RandomSource } from '@tool.io/tools/random/number';
import { validateChoice } from '../../../tool-runtime/validateChoice';
import { parseInteger } from '../../parse/integer/tool';
const BASES = ['2', '8', '16'] as const;
const PREFIXES = ['include', 'none'] as const;
export {
calculateRandomRadixValue,
type RandomRadixValue,
type SupportedRadix
};
export const randomRadixValue = (
minimumInput: unknown,
maximumInput: unknown,
widthInput: unknown,
prefixInput: unknown,
baseInput: unknown,
random: RandomSource = Math.random
): string => {
const minimum = parseInteger(minimumInput);
const maximum = parseInteger(maximumInput);
const width = parseInteger(widthInput);
const prefix = validateChoice(prefixInput, PREFIXES);
const baseChoice = validateChoice(String(baseInput), BASES);
if (
minimum === null ||
maximum === null ||
width === null ||
prefix === null ||
baseChoice === null
) {
return '';
}
const result = calculateRandomRadixValue(
minimum,
maximum,
width,
Number(baseChoice) as SupportedRadix,
prefix === 'include',
random
);
return result?.text ?? '';
};
export const Tool: TypeTool = {
name: 'Random Base Number Generator',
slug: 'random/radix-value',
intro:
'Generate a random nonnegative integer represented in binary, octal, or hexadecimal.',
tool: randomRadixValue,
i: [
{
example: '0',
label: 'Minimum',
placeholder: 'Minimum integer',
type: 'number',
value: '0'
},
{
example: '255',
label: 'Maximum',
placeholder: 'Maximum integer',
type: 'number',
value: '255'
},
{
example: '2',
label: 'Minimum Width',
placeholder: 'Minimum digit width',
type: 'number',
value: '2'
},
{
example: 'include',
label: 'Prefix',
options: [
{ text: 'Include standard prefix', value: 'include' },
{ text: 'No prefix', value: 'none' }
],
placeholder: 'Choose prefix behavior',
type: 'select',
value: 'include'
},
{
example: '16',
label: 'Base',
options: [
{ text: 'Binary (base 2)', value: '2' },
{ text: 'Octal (base 8)', value: '8' },
{ text: 'Hexadecimal (base 16)', value: '16' }
],
placeholder: 'Choose a base',
type: 'select',
value: '16'
}
],
o: [
{
example: '0x2A',
label: 'Random Base Number',
placeholder: 'Random base number will appear here...',
render: 'textarea',
type: 'text'
}
],
rerunnable: true,
alternative: [
{ title: 'Random Binary Value', url: '/random/binary' },
{ title: 'Random Octal Value', url: '/random/octal' },
{
title: 'Random Hexadecimal Value',
url: '/random/hexadecimal'
}
]
};
export default Tool;
Description
Browse the implementation files for Random Base Number Generator.