How to make a biased random number generator in VB.NET? - 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))

Related

VB.net Adding 0 in the middle of a integer

I have this integer. A = 100002 of 6 digits and i want to add 2 extra 0 in the middle so it can be a integer of 8 digits.
Result = 10000002
how can i do it?
You can split your number up into two parts, the left and the right side. Then add the zeros to the left side and put back the right side.
100002 -> 100 [left side] 002 [right side]
Dim number As Integer = 100002
Dim rightSide As Integer = number Mod 1000
Dim leftSide As Integer = number - rightSide
leftSide *= 100 ' Add zeros
Dim newNumber As Integer = leftSide + rightSide
With this, 123456 will become 12300456.
There are lots of different ways to answer this, meaning that your question is likely not specific enough to get the answer you want. For example, the_lotus's answer is of course correct (and a more practical solution, too). However, this solution will also yield the result you specify in the simplest possible manner, by subtracting 2, multiplying by 100, and adding 2 again:
Result = (A - 2) * 100 + 2
Since both of these very different methods solve the problem you have posed, it follows that you might want to pose the problem a bit more carefully. For example, if you want to work with numbers other than 100002 (which you haven't said that you do), this solution of course won't allow that. If you want a solution that applies to numbers with other than six digits, the_lotus's solution won't allow that in all cases, either.

Generate random number in Windows phone 8.1 SDK visual basic

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))

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.

Need to print multiples of 2 and 3 with three different procedures.

OK so I need some help with my vb.net class.
The question is:
Write three programs to print the multiples of 2 and 3 with three different procedures. Call those procedures with the help of delegates in one program, events in a second, and threads in a third.
My problem is not calling the functions, I can figure that out. The problem is how do I get the program to print out multiples? I think I might have to use the mod function but I am not sure; and maybe a loop so I can add a number to the variable multiple times, so then I can test to see if it has any remainders other than 0 since you find a multiple if it goes in evenly and has no remainders.
Numbers from 1 to 100 that are multiples of 2 and also multiples of 3:
For X as Integer = 0 to 100
If X mod 2 = 0 And X Mod 3 = 0 Then Debug.Print X
Next

Smalltalk dictionary as calculator

I'm working on a homework assignment that asks us to create a type of Units class that can keep track of units and perform basic arithmetic on them. The problem description has this bit, which I don't completely understand:
Probably the easiest way to keep track of the units is to give Units a dictionary that maps symbols to integers. If you are dividing by a unit then it has a negative value in the dictionary. You add two Units together by adding the value together for each symbol in the dictionary. When it is zero, throw the symbol away!
For reference, this is also included in the description:
[...] you could write an expression 3 elephants / (1 sec sec) and it would return the right thing.
Could someone shed some light here? How can I use a dictionary to map these types of units? Am I making this way harder than it needs to be?
It sounds like your teacher is giving you a hint about how to wind up with the proper units at the end of the calculation.
When you're parsing the problem, as you encounter items that are obviously units, enter them into a dictionary. The dictionary would consist of a number and a string (the supposed "unit"). Then you'd use a set of rules to increase or decrease the integer count. The resultant integer value would help you to output the units correctly.
A count of 1 indicates it's a unit in the output.
A count of -1 indicates it's inverse is a unit in the output.
A count of 0 indicates that it doesn't appear in the output at all.
Similarly, a count of 2 would indicate that it's square appears as a unit in the output.
To wit:
5 Hippo + 10 Hippo = 15 Hippos
Parsing: Dictionary:
-------- -----------
5 Hippo Hippo:1
+
10 Hippo Hippo:1 (previous operation was addition or subtraction, and already have Hippo in dictionary
But consider this problem:
5 Hippo * 5 sec/Hippo = 25 sec
Parsing: Dictionary:
5 Hippo Hippo:1
*
5 sec Hippo:1, sec:1
/
Hippo Hippp:0, sec:1 (previous operation was division of Hippo, so decrement Hippo count)
Or perhaps:
10 feet / 5 sec = 2 feet/sec
Parsing: Dictionary:
10 feet feet:1
/
5 sec feet:1, sec:-1 (divided by sec, and second is not in dictionary, so second implicitly = 0. 0 + (-1) = -1.
In the example above, feet will be on the top of the bar because it's equal to 1, and sec will be below the bar because it's value is -1. If it's value had been -2, it would have been (feet/(sec*sec) or feet/(sec squared).