iOS Random Numbers in a range - ios

I know I can get a random number for example from 0 to 700 using:
arc4random() % 362
But how can I get a random number in between for example 200 to 300?

(arc4random() % 100) + 200

Not to be picky, but I am pretty sure you have to add a number...
if you want all the numbers from 200 to and including 300... use
200 + arc4random()%101;
arc4random()%100 would give a random number from 0 to 99 so 300 would never occur...

Related

How can I flatten a pyspark dataframe joining on linked ids

I have a PySpark dataframe of ids showing the results of a series of linkage operations. It shows how the records within a series of dataframes are linked across those dataframes. An example is as below where df_a represents the first dataframe in a pair comparison and df_b the second. link_a is the id of a record in df_a that links to an entry in df_b.
So the first row is saying that dataframe 1 entry 100 links to entry 200 in dataframe 2. In the next row entry 100 in dataframe 1 links to entry 300 in dataframe 3.
df_a
df_b
link_a
link_b
1
2
100
200
1
3
100
300
1
4
100
400
1
5
100
500
2
3
200
300
2
4
200
400
2
5
200
500
3
4
300
400
4
5
400
500
1
2
101
201
1
3
101
301
2
3
201
301
2
3
202
302
1
3
103
303
1
5
103
503
In the real table there are many dataframe comparisons and across all the dataframes 100000's of links.
What I am trying to achieve is to reformat this table to show the links across all the dataframes. So you can see in the above example that 100 links to 200, 200 to 300, 300 to 400 and 400 to 500. So all 5 records link together. Not all cases will have a record in each dataframe so in some cases you will end with incomplete chains empty values.
The end result would look like.
df_1
df_2
df_3
df_4
df_5
100
200
300
400
500
101
201
301
-
-
-
202
302
-
-
103
-
303
-
503
I will then use the above to add a common id to the underlying data.
I have gotten part of the way to this using a series of joins but this seems clumsy and quickly becomes difficult to follow. I have now run out of ideas as to how to solve so assistance getting closer or even to a full solution would be gratefully received.

LUA - How to round a percentage number?

I want to round a percentage number like if is 94.5% to show it like that. But if the number is like 10.0% or 100.0% or 40.0% to show only the number 40% without the .0
In JS is Number(percentage).toFixed(1); but idk how it is in LUA.
Any ideas?
You can first round up to 1 decimal sign, and then use %g to reduce the output of insignificant zeros.
local values = {"100.0", "94.25","94.7","20.0"}
for k,v in pairs(values) do
print( string.format("%g",string.format("%.1f",v)) )
end
out:
100
94.3
94.7
20
Rounding a floatingpoint to an integer can be done by math.ceil() or math.floor()...
> for k,v in pairs({"100.0", "94.25","94.7","20.0"}) do print(math.ceil(v)) end
100
95
95
20
> for k,v in pairs({"100.0", "94.25","94.7","20.0"}) do print(math.floor(v)) end
100
94
94
20

missing data in time series

As im so new to this field and im trying to explore the data for a time series, and find the missing values and count them and study a distribution of their length and fill in these gaps, the thing is i have, let's say 10 file.txt and for each file i have 2 columns as follows:
C1 C2
944 0
920 1
920 2
928 3
912 7
920 8
920 9
880 10
888 11
920 12
944 13
and so on... lets say till 100 and not necessarily the 10 files have the same number of observations.
so here for example the missing values and not necessarily appears in all files that i have, missing value are: 4,5 and 6 in C2 and the corresponding 1st column C1(measured in milliseconds, so the value of 928ms is not a time neighbor of 912ms). So i want to find those gaps(the total missing values in all 10 files) and show a histogram of their lengths.
i wrote a piece of code in R, but the problem is that i don't get the exact total number that i should have for the missing values.
path = "files path"
out.file<-data.frame(TS = 0, Index = 0, File = '')
file.names <- dir(path, pattern =".txt")
for(i in 1:length(file.names)){
file <- cbind(read.table(file.names[i],
header=F,
sep ="\t",
stringsAsFactors=FALSE),
file.names[i])
colnames(file) <- c('TS', 'Index', 'File')
out.file <- rbind(out.file, file)
}
d = dim(out.file)[1]
misDa = 0
for(i in 2:(d-1)){
if(abs(out.file$Index[i]-out.file$Index[i+1]) > 1)
misDa = misDa+1
}
Hard to give specific hints without having a more extensive example of your data that contains some of the actual NAs.
If you are using R (like it seems) the naniar and the imputeTS packages offer nice functions for missing data visualizations.
Some examples from the naniar package, which is especially good for multivariate data (more plot examples):
Some examples from the imputeTS package, which is especially good for time series data (additional plot examples):

Parsing Text FIle Using Awk

I would like to parse a text file that has the section of interest as follows:
mesh 0 400 12000
400 300 400
1 0 -1
300 500 600
0 0 1
etc....
12000
1300
1100
etc..
I would only like the rows that immediately follow the row that starts with string mesh and every other one after that as well, and has 3 columns. I would like this output to be in a separate text file with a modified name.
So desired output text file:
400 300 400
300 500 600
I tried to do this with python and loops but it literally took hours and never finished as there are thousand to hundred of thousands of lines in the original text file.
Is there a more efficient way to do this in with a bash script using awk?
awk to the rescue!
$ awk '/^mesh/{n=NR;next} NF==3 && n && NR%2==(n+1)%2' file > filtered_file
400 300 400
300 500 600

Multiple MySql WHERE Between Clauses

Newbie MySql programmer thanks for the patience.
Im trying to track an Id number in a table where 3 different conditions are met this is what Iv got however the query dosent return any results where there are clearly matches in the table. Thoughts?
SELECT *
FROM `table`
WHERE `x` BETWEEN 80 AND 20
AND `y` BETWEEN 120 AND 20
AND `z` BETWEEN 40 AND 10
LIMIT 0 , 30
Am I right in theory to think that this should work?
Close, but no cigar. :)
SELECT * FROM table
WHERE (x BETWEEN 20 AND 80) AND
(y BETWEEN 20 AND 120) AND
(z BETWEEN 10 AND 40) LIMIT 0 , 30
To explain, SQL servers generally evaluate x BETWEEN val1 AND val2 the same as x >= val1 AND x <= val2. The way your original query was written, the first condition would be x >= 120 AND x <= 20), which obviously wasn't what you intended.
The parentheses around the different conditions make sure that each is evaluated completely before the AND is considered. It makes a difference most of the time in SQL, and even when it doesn't it's a good idea to use them so your intentions are clear 6 months from now when you (or someone else) has to look at the query again.
SELECT * FROM table WHERE
(x BETWEEN 20 AND 80) AND
(y BETWEEN 20 AND 120) AND
(z BETWEEN 10 AND 40)
LIMIT 0 , 30
I think the range needs to be the other way around:
SELECT * FROM table WHERE x BETWEEN 20 AND 80 AND y BETWEEN 20 AND 120 AND z BETWEEN 10 AND 40 LIMIT 0 , 30

Resources