In Kotlin, I have an expression that produces random Float values. Write an expression…
- User
In Kotlin, I have an expression that produces random Float values. Write an expression that does the same but excluding infinities and NaN.
Assistant
Here are a couple of approaches to generate random Float values while excluding infinities and NaN:
Option 1: Filter approach (regenerate if needed)
fun nextFiniteFloat(): Float { var value: Float do { value = Random.nextFloat() // or your existing random expression } while (!value.isFinite()) return value }Option 2: Bounded generation
// Generate random float in the full finite range Random.nextFloat() * (Float.MAX_VALUE - (-Float.MAX_VALUE)) + (-Float.MAX_VALUE) // Or for a more practical range (e.g., -1000.0 to 1000.0) Random.nextFloat() * 2000f - 1000f
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.