package gen import "math/rand" import "github.com/firestuff/subcoding/vm" func randOperand(t vm.OperandNumericType) *vm.Operand { for { op := &vm.Operand{ Type: randOperandType(), } switch op.Type { case vm.GlobalMemoryIndex: op.Value = RandBiasedUint64n(vm.GlobalMemorySize) case vm.FunctionMemoryIndex: op.Value = RandBiasedUint64n(vm.FunctionMemorySize) case vm.Literal: switch t { case vm.OperandUnsigned: op.Value = RandBiasedUint64() case vm.OperandSigned: op.Value = RandBiasedInt64() case vm.OperandSignedOrUnsigned: op.Value = RandBiasedUSint64() case vm.OperandReference: // Invalid. Roll the dice again. continue } } return op } } var operandTypes = []vm.OperandType{ vm.Literal, vm.FunctionMemoryIndex, vm.GlobalMemoryIndex, } func randOperandType() vm.OperandType { // Uniform distribution return operandTypes[rand.Intn(len(operandTypes))] }