Generate random number in Windows phone 8.1 SDK visual basic - vb.net

I am a student and developing a quiz type of project. And I get the problem that I can't get random numbers for the questions.
The idea is to generate a number and than pick one of the texts (1,2,3) and according to the text to have the right number as answear. Also I need it in VB

If what you want to do is generate a random number you can use Random and the Next method:
The Next(Int32, Int32) overload returns random integers that range from minValue to maxValue – 1. However, if maxValue equals minValue, the method returns minValue.
Here is some sample code:
Dim r As Random = New Random
'The values 1, 2 and 3 are possible.
Console.WriteLine(r.Next(1, 4))
Console.WriteLine(r.Next(1, 4))
Console.WriteLine(r.Next(1, 4))

Related

Confused Beginner learning Python

I am working on a problem in Python and don't understand the answer.
for number in range(1, 10):
if number % 2 == 0:
print(number)
The answer to this problem is 2,4,6,8
Can anyone explain this answer?
range is a function in python which generates a sequence of integers, for example:
r=range(3)
returns a iterable object range(0,3) which generates sequence of integers from 0 to 3-1(2),inorder for you to see the elements in it , you can loop through it:
for i in r:
print(i)
#prints number from 0 to 3-1
Or, wrap it in a list:
list(range(3)) //returns [0,1,2]
range can take 3 params as input start,end and optionally step.The parameters start and end are basically lower and upper bounds to the sequence.In the above example since we have given only one integer range considers start as 0 and end as 3. This function range(start,end,[step]) generates integers in the following manner: start,start+1....end-1 considering the above example 0,0+1...3-1
if you give both the start and the end params to the range, the function generates integers from start upto but not including end, Example:
for i in range(3,8):print(i) #prints numbers from 3 to 8-1
if you give the third parameter which is the step(which is usually 1 by default), then range adds that number to the sequence :
list(range(3,8)) or list(range(3,8,1)) # will return [3,4,5,6,7],sequence generation will be like:3,3+1,(3+1)+1...
list(range(3,8,2)) #returns [3,5,7];3,3+2,(3+2)+2....
So , coming to your question now :
for number in range(1, 10): if number % 2 == 0: print(number)
In the above code you are basically telling python to loop over the sequence of integeres between 1 to 9 and print the numbers which are divisible by 2,which prints 2,4,6,8.
Hope this helped you :)

Find index of an ordered set of N elements

Problem description:
A set of lists of N integers i1,i2,....,iN with 0<= i1<=i2<=i3<=....<=iN <=M, is created by starting with one integer 0<=i1<=M, and repeatedly adding one integer that is greater or equal to the last integer added.
When adding the last integer to get the final set of lists, the index runs starting from 0 to BinomialC[M+N,N)]-1.
For example, for M=3, i1=0,1,2,3
so the lists are
{0},{1},...,{3}.
Adding another integer i2>=i1 will result in
{0,0},{0,1},{0,2},{0,3},
{1,1},{1,2},{1,3},
{2,2},{2,3}
{3,3}
with indices
0,1,2,3,
4,5,6,
7,8,
9.
This index can be represented in terms of i1,i2,...,iN and M. If the conditions >= were not present, then it would be simply i1*(M+1)^(N-1)+i2*(M+1)^(N-2)+...+iN*(M+1)^(N-N). But, in the case above, there is a negative shift in the index due to the restrictions. For example, N=2 the shift is -i1(i1+1)/2 and index is i = i1*(M+1)^1 + i2*(M+1)^0 -i1(i1+1)/2.
Question:
Does anyone especially from mathematics background knows how to write the index for general N element case? or just the final expression? Any help would be appriciated!
Thanks!

Convert cell text in progressive number

I have written this SQL in PostgreSQL environment:
SELECT
ST_X("Position4326") AS lon,
ST_Y("Position4326") AS lat,
"Values"[4] AS ppe,
"Values"[5] AS speed,
"Date" AS "timestamp",
"SourceId" AS smartphone,
"Track" as session
FROM
"SingleData"
WHERE
"OsmLineId" = 44792088
AND
array_length("Values", 1) > 4
AND
"Values"[5] > 0
ORDER BY smartphone, session;
Now I have imported the result in Matlab and I have six vectors and one cell (because the text from the UUIDs was converted in cell) all of 5710x1 size.
Now I would like convert the text in the cell, in a progressive number, like 1, 2, 3... for each different session code.
In Excel it is easy with FIND.VERT(obj, matrix, col), but I do not know how do it in Matlab.
Now I have a big cell with a lot of codes like:
ff95465f-0593-43cb-b400-7d32942023e1
I would like convert this cell in an array of numbers where at the first occurrence of
ff95465f-0593-43cb-b400-7d32942023e1 -> 1
and so on. And you put 2 when a different code appear, and so on.
OK, I have solve.
I put the single session code in a second cell C.
At this point, with a for loop, I obtain:
%% Converting of the UUIDs into integer
C = unique(session);
N = length(session);
session2 = zeros(N, 1);
for i = 1:N
session2(i) = find(strcmp(C, session(i)));
end
Thanks to all!

How to make a biased random number generator in VB.NET?

How do I make a biased random number generator (RNG) in VB.NET?
I know I could make it by fiddling with the output of the Randomize()/Rnd methods, but is there a built-in way of doing this?
I want the biased RNG to give me either a 2 or 4 (though using 1 or 2 as a substitute is also OK by me), with 2 occurring on average 90% of the time and 4 occurring on average 10% of the time.
Create a random number generator to return values from 1-10, if the value from the random number generator is between 1 and 9 send a 2 if the value is 10 send a 4.
You might want to look at this
http://msdn.microsoft.com/en-us/library/vstudio/ctssatww(v=vs.100).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-2
If you want to come out with a mask to generate your values
Here is what I think you can do.
Dim numbers() as integer = {2,2,2,2,4,2,2,2,2,2} ' set 10% for 4, 90% for 2
Dim r as new Random()
Return numbers(r.Next(0, 10))

Generating Even Random Numbers

I need a code to generate only random EVEN numbers 2-100
There is tutorials on the web that generate random numbers but they're odd and even.
Please understand i just need even numbers to generate.
1, generate numbers 1-50
2, multiply all the numbers by 2
all numbers multiplied by 2 are even
This will work:
NSInteger evenNumber = (arc4random_uniform(50) + 1) * 2;
arc4random_uniform(50) will give results in the range 0 - 49. Adding 1 gives a value in the range 1 - 50. Multiplying by two gives you even numbers in the range 2 - 100.