I have some code that produces random numbers for an AI but I am unsure how the percentage sign works.
This is my code that creates the numbers:
ey[i]=rand()%950;
ex[i]=rand()%300+800;
ev[i]=rand()%10-5;
Im using C++ in Visual Studio
Thanks in advance
The percentage is usually a Modulus operator, which you can think of as the remainder after division.
For example
10 % 3 = 1
because 3 goes in to 10 3 times with 1 left over.
Your code gets a random number and then divides it by the right number, saving the remainder.
Related
Given :
I was given a function that generates randomly 0 or 1. It generates 0 with probability p and 1 with probability 1-p.
Requirement:
I need to create a function that generates a number between 0 and 6 randomly with uniform probability by utilizing the above given function.
Note:cant use inbuilt random functions.
Can someone help me with this.
Thanks in advance
You can skew a biased random function to become unbiased by checking for a sequence of 01 or 10 and ignoring other results, this way you have a fair coin with a 50% chance of outputting any of the said sequences ((1-p)*p == p*(1-p)
With this fair coin you can then roll 3 bits and output the rolled number, if you roll a 7 (111) just repeat the process.
I need to know the name of this function, please help.
The idea of this function is that you have some number and you need to finding two biggest numbers from fibonacci sequence which you need to add to get this number. And method returns to you array of this fibonacci digits but the numbers in this array represented by 0 and 1 where two numbers 1 and all others 0. And the position of 1 in array is the same where the biggest numbers as in fibonacci seq.
For example i have fibonacci sequence {1,1,2,3,5,8,13}
Number = 11 so the two numbers from sequence will be 8 and 3
The output will be {0,0,0,1,0,1,0}
Its pretty famous as i remember and I'm not completeley sure but using of fibonacci is one of the solutions of it. Please help to get the name of it so i could find out more about it
This is not function, but numeral system: Fibonacci coding - representation of integer as sum of Fib. numbers
I'm making a hash function for a hash table of size 10 (indexes 0-9), and hashing elements using mid-square method.
The Problem is I'm confused whether i should use 1 middle digit or 2 digits then taking mod 10 of it.
The problem is if I choose to take 2 mid digit then taking mod 10, this method will fail if the squared number has 3 digits. Which two digits will I take then?
and if I choose to have 1 middle digit, then I'll have problem taking mid digit when squared number has even number of digits. I know in this situation both of the mid two element are made from contribution of all elements of the original number.
Right now, I'm going with {floor(n/2+1)}th digit. This way it works for three digit squared number, and when the squared number has even no. of digits, it's taking the later digit of the middle two.
I wanna know what more efficient approach of doing this?
Thanks.
I was taught that the correct way to mid square hash is to use 1 digit if the length of the resulting square is odd, if it's even then use the middle 2 digits.
I have to make a recursive function that finds the number of ones from 0 to n recursively.
So f(16) = 9
(1,10,11,12,13,14,15,16)
This is obviously homework so I would appreciated if you did NOT post any code, just the reasoning behind it.
What I've reasoned so far is that if you do %10 of a number it will tell you if the least significant is a one, also if you do an integer division by 10 you lose that digit.
So I'm guessing the approach will be can be checking if number%10 == 1 and then calling the function with f(n/10), but then I get lost in the actual implementation.
I would appreciate if you could comment what approach would you use, it has to be recursive just because it's home work, the procedural approach was trivial.
For these types of problems, I find it helps to write some sort of diagram showing the patterns. For instance, if you count by tens, you know that the first set (0-9) contains one 1.
(0-9) -- 1
(10-19) -- 11
(21-29) -- 1
| | -- 1
(100-109) -- 11
(110-119) -- 21
(120-129) -- 11
| | -- 11
(200-209) -- 1
(210-219) -- 11
(220-229) -- 1
| | -- 1
...
(1000-1009) etc...
It may take a while, but this will help you find patterns so you can come up with a more sytematic answer. I don't want to give you too much help since it's a homework problem, but that's the approach I take when I'm solving creative math problems.
You've got two parts to your problem.
Find numbers of ones in number.
Find numbers of ones in all the numbers less than it (but more than zero).
Part one first:
If the right-most digit is 1, then number % 10 == 1.
If the number is > 9 you need to check other digits, you can do this by doing the same test on the number after integer-divide 10. If the number <= 9, then that would give you zero.
So your OnesInNumber function is a bit like:
If number == 0, return 0.
Otherwise call OnesInNumber on number / 10.
If number % 10 == 1 add 1 to that result.
Return the result.
This will for example, give you 1 when called on 10, 1, 12, 303212, give you 2 when called on 11, and so on.
Your OnesInZeroUntil function is then like:
If number <= 0, return 0.
Otherwise call OnesInZeroUntil on number - 1.
Add OnesInNumber(number) to this.
Return the result.
So you've a recursive function that works out the number of 1 in a number, and another recursive function that works out the number of 1 in every number up to that one, building on that first function.
That'd be enough to write a quick 2 functions in, had you not requested that we don't.
(Tip: If your teacher isn't already requiring it, see if you can work out how to do this without recursion. Every recursive function can be re-written as a non-recursive form, and it's a practical skill to be able to do that some people teaching recursion don't seem to cover).
You are quite right in your approach. For every number you want a function that will return the "number of ones" in the decimal representation. A recursive representation of this (note you could also do this iteratively).
Like all recursive functions, you need your end-state catch, i.e., if the input = 0 return 0. Besides that (without giving it all away) you just need to add your current result to the sub-result:
if number==0
return 0
if number%10==1
return myFunc(number/10) + 1
else
return myFunc(number/10)
However, as I said before, there is no need to use recursion. An iterative solution is probably better here since the function is linear with respect to the number of digits.
Out of pure interested, I'm curious how to create PI sequentially so that instead of the number being produced after the outcome of the process, allow the numbers to display as the process itself is being generated. If this is the case, then the number could produce itself, and I could implement garbage collection on previously seen numbers thus creating an infinite series. The outcome is just a number being generated every second that follows the series of Pi.
Here's what I've found sifting through the internets :
This it the popular computer-friendly algorithm, The Machin-like Algorithm :
def arccot(x, unity)
xpow = unity / x
n = 1
sign = 1
sum = 0
loop do
term = xpow / n
break if term == 0
sum += sign * (xpow/n)
xpow /= x*x
n += 2
sign = -sign
end
sum
end
def calc_pi(digits = 10000)
fudge = 10
unity = 10**(digits+fudge)
pi = 4*(4*arccot(5, unity) - arccot(239, unity))
pi / (10**fudge)
end
digits = (ARGV[0] || 10000).to_i
p calc_pi(digits)
To expand on "Moron's" answer: What the Bailey-Borwein-Plouffe formula does for you is that it lets you compute binary (or equivalently hex) digits of pi without computing all of the digits before it. This formula was used to compute the quadrillionth bit of pi ten years ago. It's a 0. (I'm sure that you were on the edge of your seat to find out.)
This is not the same thing as a low-memory, dynamic algorithm to compute the bits or digits of pi, which I think what you could mean by "sequentially". I don't think that anyone knows how to do that in base 10 or in base 2, although the BPP algorithm can be viewed as a partial solution.
Well, some of the iterative formula for pi are also sort-of like a sequential algorithm, in the sense that there is an iteration that produces more digits with each round. However, it's also only a partial solution, because typically the number of digits doubles or triples with each step. So you'd wait with a lot of digits for a while, and the whoosh a lot more digits come quickly.
In fact, I don't know if there is any low-memory, efficient algorithm to produce digits of any standard irrational number. Even for e, you'd think that the standard infinite series is an efficient formula and that it's low-memory. But it only looks low memory at the beginning, and actually there are also faster algorithms to compute many digits of e.
Perhaps you can work with hexadecimal? David Bailey, Peter Borwein and Simon Plouffe discovered a formula for the nth digit after the decimal, in the hexadecimal expansion of pi.
The formula is:
(source: sciencenews.org)
You can read more about it here: http://www.andrews.edu/~calkins/physics/Miracle.pdf
The question of whether such a formula exists for base 10 is still open.
More info: http://www.sciencenews.org/sn_arc98/2_28_98/mathland.htm