Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8009709
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T18:31:28+00:00 2026-06-04T18:31:28+00:00

I would like to to sample values from a vector s<-0:1440 to create a

  • 0

I would like to to sample values from a vector s<-0:1440 to create a vector u so that the sum(u)=x while length(u)<k, for given k and x. Obviously k*max(s)>sum(u).

Is there any way to brute force simulate numerous such u vectors? I would like to avoid messing with probability distributions (for the sampling) and I don’t care if some u vectors will get discarded.

EDIT: Regarding P Lapointe’s nice comment about the length(u). It is important that the length(u) should not be fixed (length(u)<k) so that the vectors u are of variable lengths. Another approach would be to fix the length(u)=k, but the algorithm should be able to randomly insert (simulate) zeros in the u vectors. This would have the consequence that by adding a zero, the sum(u) remains the same but the length(u) increases by one (until length(u)=k). It is important that the zeros appear randomly (not just at the end of the simulated vector, just to satisfy length(u)=k)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T18:31:30+00:00Added an answer on June 4, 2026 at 6:31 pm

    OK, here’s an algo that answers your question. Basically, we are doing two random samples. The first one to find a k that satisfies the length(u)<k constraint. Using that k, we then use another sample to find k-1 numbers. This is called initial in the algo. When we find a sample k-1 which is lower than x, the desired sum, we add the difference of x-sum(initial) to complete the series.

    #Inputs
    x <-2500 # desired sum
    s1<-0:min(1440,x) #universe
    max.k <-10
    
    k <-sample(3:(max.k-1),1) #length(u)<k, starts at 3 because low k can be problematic 
                              #with current inputs
    initial <-x+1 #deliberately above limit to initialize the while
    u <-s1+1      #deliberately above limit to initialize the while
    
    while (sum(initial)>x | max(u)>max(s1)) {
    initial <-sample(s1,k-1,replace=TRUE) #Find k-1 samples
    u <-c(initial,x-sum(initial)) #add number that makes sum == x
    }
     #example 
     > k
    [1] 4
    > x
    [1] 2500
    > u
    [1]  282 1337  876    5
    > sum(u)
    [1] 2500
    

    Also, if you have a large max.k, it might be a good thing to add a probability vector that gives more probability to low numbers in the sample. Otherwise, in the current example, it is tough to get a sum==2500 if you have several numbers above 1000.

    prob1 <-1/((s1+1)*max.k ) #gives more probality to low numbers
    
    while (sum(initial)>x | max(u)>max(s1)) {
    initial <-sample(s1,k-1,replace=TRUE,prob=prob1) #Find k-1 samples
    u <-c(initial,x-sum(initial)) #add number that makes sum == x
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to create a simple control that inherits from HeaderedContentControl, and has
I would like to extract items from this sample html, more specificly, i would
Using the Northwind sample: I would like to create an insert trigger on the
I would like to replace values in a vector ( x ) with values
Update: sample file sample workbook Problem : I would like to have something that
Hi i would like to create an array from the title and src of
I have a sample dataform and I would like to remove the ability to
I have this sample string where I would like to replace the star with
I would like to create simple objects at runtime (textbox, label, etc) and add
This method that uses method-level generics, that parses the values from a custom POJO,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.