简体   繁体   English

如何将数字分解为随机生成的部分

[英]How to break a number into randomly generated parts

I want to make a div that is full width on the page.我想在页面上制作一个全宽的 div。 It's a container.它是一个容器。 Then I want to fill it in with divs, and width of each div is 50*n, where n is a randomly generated number.然后我想用div填充它,每个div的宽度是50*n,其中n是一个随机生成的数字。 Assume I have a container div with width of 1240px.假设我有一个宽度为 1240px 的容器 div。 Now I run a JS function that fills it with eg 10 divs with different widths.现在我运行一个 JS function 来填充它,例如 10 个不同宽度的 div。 Now if I sum up all inner divs widths, I get 1240px.现在,如果我总结所有内部 div 的宽度,我得到 1240px。

This way I always know that when I run filling function, I get a collection of divs that altogether always are 1240px.这样我总是知道当我运行填充 function 时,我得到了一个总大小为 1240px 的 div 集合。 Number of divs shouldn't be a fixed number, so there can be 4 or 7 divs. div 的数量不应该是一个固定的数字,所以可以有 4 或 7 个 div。 The number of divs amount is generated randomly. div的数量是随机生成的。 Then, if there is some space left, for 1240 px this number is 40px I suppose, it is filled with some dummy div that doesn't have to be of 50*n width.然后,如果还有一些空间,对于 1240 像素,我想这个数字是 40 像素,它充满了一些虚拟 div,不一定是 50*n 宽度。

I have created a function but it doesn't always work as supposed to.我创建了一个 function 但它并不总是按预期工作。

function generateItems() {
    originalwidth = $(document).width() - 40;
    var fullwidth = 0;
    var counter = 0;
    do{
        var randomnumber = 1 + Math.floor(Math.random() * 4);
        tempwidth = 50 * randomnumber;
        fullwidth += tempwidth;
        if (fullwidth > originalwidth) {
            $('#questions').append('<div class="question-area" style="width:' + (originalwidth - fullwidth) + 'px;"><strong>' + (originalwidth - fullwidth) + '</strong></div>');
            break;
        }
        width_padding = tempwidth;
        $('#questions').append('<div class="question-area" style="width:' + width_padding + 'px;">' + width_padding + '</div>');
        counter++;
    }
    while (true);
}

I am not even sure it's a good way I have chosen to solve such a task.我什至不确定这是我选择解决此类任务的好方法。 Please share your thoughts on how to better do this.请分享您对如何更好地做到这一点的想法。

I've refactored the code from your answer a bit.我已经从您的答案中重构了代码。

See: http://jsfiddle.net/thirtydot/Bk2yw/见: http://jsfiddle.net/thirtydot/Bk2yw/

function generateItems() {
    var slotWidth = 50,
        maxSlots = 3,
        thisSlotNum, thisWidth;
    var remainingSlots = Math.floor($('#questions').width() / slotWidth),
        remainingWidth = $('#questions').width() % slotWidth;

    while (remainingSlots > 0) {
        thisSlotNum = Math.min(remainingSlots, 1 + Math.floor(Math.random() * maxSlots));
        remainingSlots -= thisSlotNum;

        thisWidth = thisSlotNum * slotWidth;
        $('#questions').append('<div class="question-area" style="width:' + thisWidth + 'px;"><strong>' + thisWidth + '</strong></div>');
    }
    if (remainingWidth) {
        $('#questions').append('<div class="question-area" style="width:' + remainingWidth + 'px;"><strong>' + remainingWidth + '</strong></div>');
    }
}

I played a little more with the code and it seems I made it work properly, though I am sure there is something to improve.我对代码进行了更多尝试,似乎我让它正常工作,但我确信还有一些需要改进的地方。

Here is the code:这是代码:

function generateItems() {
    originalwidth = $(document).width() - 40;
    $('#questions').css('width', originalwidth);
    var fullwidth = 0;
    var counter = 0;
    do{
        var randomnumber = 1 + Math.floor(Math.random() * 4);
        tempwidth = 50 * randomnumber;
        fullwidth += tempwidth;
        if (fullwidth > originalwidth) {
            $('#questions').append('<div class="question-area" style="width:' + (originalwidth + tempwidth - fullwidth) + 'px;"><strong>' + (originalwidth + tempwidth - fullwidth) + '</strong></div>');
            break;
        }
        width_padding = tempwidth;
        $('#questions').append('<div class="question-area" style="width:' + width_padding + 'px;">' + width_padding + '</div>');
        counter++;
    }
    while (true);

}

Here is an screenshot of how it works.这是它如何工作的屏幕截图。 The bottom row is the result for another width of the page.底行是页面另一个宽度的结果。在此处输入图像描述

A couple of problems:几个问题:

fullwidth += tempwidth;
if (fullwidth > originalwidth) {
    $('#questions').append('<div class="question-area" style="width:' + (originalwidth - fullwidth) + 'px;"><strong>' + (originalwidth - fullwidth) + '</strong></div>');
    break;
}

Here you have already "gone over" the original width with the fullwidth variable, so when you do originalwidth - fullwidth it's always negative (by definition because fullwidth > originalwidth ).在这里,您已经使用 fullwidth 变量“超越”了原始宽度,因此当您执行originalwidth - fullwidth时,它始终为负数(根据定义,因为fullwidth > originalwidth )。

Another problem is that if the random width is for example 150 pixels and you have 140 pixels of space left, your code considers that as space running out and puts in the dummy div of 140 px.另一个问题是,如果随机宽度例如为 150 像素,而您还有 140 像素的空间,则您的代码会将其视为空间用完并放入 140 像素的虚拟 div。 It's not clear from your question but you probably want a 100 px block and the rest filled with a 40 px block.您的问题尚不清楚,但您可能想要一个 100 px 的块,而 rest 填充一个 40 px 的块。

Here's a working version:这是一个工作版本:

function generateItems() {
    var originalwidth = $(document).width() - 40;
    var fullwidth = 0;
    var counter = 0;
    do{
        var randomnumber = 1 + Math.floor(Math.random() * 3);
        var tempwidth = 50 * randomnumber;
        if (fullwidth + tempwidth > originalwidth) {
            var diff = originalwidth - fullwidth;
            if( originalwidth - fullwidth > 50 ) {
                tempwidth = diff - ( diff % 50 );
            }
            else {
                $('#questions').append('<div class="question-area" style="width:' + diff + 'px;"><strong>' + diff + '</strong></div>');
                break;
            }
        }
        fullwidth += tempwidth;
        $('#questions').append('<div class="question-area" style="width:' + tempwidth + 'px;">' + tempwidth + '</div>');
        counter++;
    }
    while (true);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将数字与随机生成的数字匹配并显示匹配的位数 - match the number with randomly generated number and display how many digits matched 如何生成一个可以被另一个随机生成的数整除的随机数 - How to generate a random number that is evenly divisible by another randomly generated number 随机生成的数组中的最大数量 - The max number in a randomly generated array 如何阻止随机生成的数字刷新表单提交? - How to stop randomly generated number from refreshing on form submit? 单击后如何使用 JavaScript 再次重置随机生成的数字? - How to reset a randomly generated number again after a click using JavaScript? 如何用随机生成的数字替换window.location的一部分? - How to replace a portion of window.location with a randomly generated number? 如何使函数随机运行多次 - How to make a function run a number of times that is randomly generated 如何为HTML 5和javascript中的简单猜数字游戏随机设置生成的数字 - How to set a randomly a generated number for a simple guess the number game in HTML 5 and javascript 如果随机生成的数字在 Javascript 中是不受欢迎的数字,我该如何重新生成? - How do I regenerate a randomly generated number if it's an undesirable number in Javascript? 想要在 JavaScript 中对随机生成的名称列表进行编号 - Wanting to number a list of randomly generated names in JavaScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM