# How do you drop items in a list that's created through a loop?

Mathematica Asked on December 18, 2020

I have the following lines of code:

randomnumbers = {10};
For[w = 1, w <= 10, w++,
randomnumbers = Append[randomnumbers, RandomInteger];
addition = Last[randomnumbers] + Last[Drop[randomnumbers, -1]];

Print[randomnumbers];
]


Basically, I have a list given by randomnumberswhere the first number is 10. Every iteration of the loop adds a random integer between 0 – 10 to randomnumbers. Once that number is added, I then do addition and I add the newly appended number to the number immediately before it. The issue is that every time the loop runs, randomnumbersstores all the previously added values. What I would like to do is that once additionis performed, the number in position 1 of randomnumbers is dropped. That way at any one time, there are only a max of two numbers in the list made by randomnumbers the newly appended number and the one immediately before it.

As it currently runs the outputs look something like this:

{10,1}
11
{10,1,2}
3
{10,1,2,5}
7
etc


but I would like an output that runs like what I’ve written below, where the list is not continuously increasing.

{10,1}
11
{1,2}
3
{2,5}
7
etc


I have tried adding in Drop[randomnumbers,1]after addition, but that doesn’t seem to continuously remove the number in position 1 of randomnumbers

The answer you are looking for is this one:

randomnumbers = {10};
For[w = 1, w <= 10, w++,
If[w > 1, randomnumbers = Drop[randomnumbers, 1];
randomnumbers = Append[randomnumbers, RandomInteger];,
randomnumbers = Append[randomnumbers, RandomInteger];];
addition = Last[randomnumbers] + Last[Drop[randomnumbers, -1]];


P.S. I don't know yet how to properly write a code as an answer, I apologize for that, it is my second day using StackExchange forums.

Answered by PanchoTheMacho on December 18, 2020

randomnumbers = {10};
For[w = 1, w <= 10, w++,
randomnumbers = {Last[randomnumbers], RandomInteger};
Print[randomnumbers];


Answered by Chris Degnen on December 18, 2020

Note you could this with NestList, e.g.

fun[n_] :=
With[{s = Rest[NestList[{#[], RandomInteger} &, {0, 10}, n]]},
Thread[{s, Total /@ s}]]


A list of 10 fun:

{{{10, 0}, 10}, {{0, 7}, 7}, {{7, 6}, 13}, {{6, 1}, 7}, {{1, 7},
8}, {{7, 6}, 13}, {{6, 6}, 12}, {{6, 10}, 16}, {{10, 10}, 20}, {{10, 3}, 13}}

Answered by ubpdqn on December 18, 2020

## Related Questions

### Confidence interval

1  Asked on September 5, 2021

### How to implement split-complex numbers?

2  Asked on September 5, 2021 by volcanrb

### Fast approach to do Summation in Compile[]?

1  Asked on September 5, 2021

### How to chat with yourself?

0  Asked on September 5, 2021

### How to antialias Plot3D graphics?

2  Asked on September 5, 2021

### FunctionRange, MinValue and MaxValue unable to give result

2  Asked on September 4, 2021

### Compiled Function is slower than non-compiled version?

1  Asked on September 4, 2021

### How to make the $y$-axis of a spectrogram logarithmic?

1  Asked on September 4, 2021

### How to generate a large number of constraints for all indices ($forall i$)

0  Asked on September 3, 2021

### How to color a plot with a vector

1  Asked on September 3, 2021

### How can I improve the results of numerical integration using NIntegrate?

2  Asked on September 3, 2021

### How can I obtain the matrices of coefficients of quadratic form?

1  Asked on September 3, 2021

### How to insert letters in a “word”?

2  Asked on September 3, 2021

### Synchronising time series and video display

1  Asked on September 3, 2021

### Remove or delete elements

1  Asked on September 3, 2021 by y-l

### Generating points inside convexhullmesh

1  Asked on September 3, 2021

### ToElementMesh Error/General meshing strategies

1  Asked on September 2, 2021

### Multivariate linear regression

3  Asked on September 2, 2021

### How to apply ColorFunction only to data visible within PlotRange?

1  Asked on September 2, 2021 by ca-trevillian

### Fitting an integral function given a set of data points

1  Asked on September 2, 2021

### Ask a Question

Get help from others!