Что самый оптимальный путь состоит в том, чтобы получить случайное число с плавающей точкой между floatA и floatB?

Я решил проблему, решение состоит в том, чтобы изменить подсветку, отмену выделения и, прежде всего, методы errorPlacement для каждого экземпляра проверки. Однако, чтобы не изменять каждый файл, я помещаю изменения в файл jquery.validate.js

highlight: function( element, errorClass, validClass ) {
        if ( element.type === "radio" ) {
            this.findByName( element.name ).addClass( errorClass ).removeClass( validClass );
        } else {
            var elem = $(element);
            if (elem.attr('readonly') == 'readonly') {
                if (elem.hasClass("input-group-addon")) {
                   $("#" + elem.attr("id")).parent().addClass(errorClass);
                } else {
                    $( element ).addClass( errorClass ).removeClass( validClass );
                }
            } else {
                if (elem.hasClass("select2-hidden-accessible")) {
                   $("#select2-" + elem.attr("id") + "-container").parent().addClass(errorClass);
                } else {
                    $( element ).addClass( errorClass ).removeClass( validClass );
                }
            }
        }
    },
    unhighlight: function( element, errorClass, validClass ) {
        if ( element.type === "radio" ) {
            this.findByName( element.name ).removeClass( errorClass ).addClass( validClass );
        } else {
            var elem = $(element);
            if (elem.attr('readonly') == 'readonly') {
                if (elem.hasClass("input-group-addon")) {
                   $("#" + elem.attr("id")).parent().removeClass(errorClass);
                } else {
                    $( element ).addClass( errorClass ).removeClass( validClass );
                }
            } else {
                if (elem.hasClass("select2-hidden-accessible")) {
                    $("#select2-" + elem.attr("id") + "-container").parent().removeClass(errorClass);
                } else {
                    $( element ).removeClass( errorClass ).addClass( validClass );
                }
            }
        }
    },
    errorPlacement: function(error, element) {
        var elem = $(element);
        if (elem.attr('readonly') == 'readonly') {
            element = $("#" + elem.attr("id")).parent();
            error.insertAfter(element);
        } else {
            if (elem.hasClass("select2-hidden-accessible")) {
                element = $("#select2-" + elem.attr("id") + "-container").parent().parent().parent();
                error.insertAfter(element);
            } else {
                error.insertAfter(element);
            }
        }
    }
6
задан Thanks 20 May 2009 в 10:57
поделиться

2 ответа

try this

float a = 12.49953f;
float b = 39.11234f;
int startVal = a*10000;
int endVal = b*10000; 

srandom((unsigned)(mach_absolute_time() & 0xFFFFFFFF));
int randomValue = startVal+ (random() % (endVal - startVal));

float r = (float)randomValue / 10000.0f; 
5
ответ дан 16 December 2019 в 21:45
поделиться

Generally you get a random number between 0 and 1 (1 excluded) by the random function. This can be uniformly spread to the required interval by multiplying the random number with the interval-length between a and b and adding the lower to it.

with a

float r = random();
return a+(b-a)*r;

For the problem with 20 different random numbers: with 20 numbers - i'd create some kind of array and do a linear search each time as you suggested. This is easy to implement without failure and easy to debug. This saves you time solving problems that are more complicated. 20 is low enough for the linear search to have no major performance impact.

3
ответ дан 16 December 2019 в 21:45
поделиться