Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
Using C Language:
How can I generate random numbers in the [pi, 2pi] range?
How can I generate random numbers in the [-1, 0] range?
This is a pseudo random integer generator, but has only been tested for positive numbers, you would have to modify it to use with negatives:
int randomGenerator(int min, int max)
{
int random=0, trying=0;
trying = 1;
srand(clock());
while(trying)
{
random = (rand()/32767.0)*(max+1);
(random >= min) ? (trying = 0) : (trying = 1);
}
return random;
}
EDIT (last method did not produce randoms due to srand() not being updated enough)
For a range spanning positive and negative numbers you could modify it like this: (but ratio of pos to neg would be same)
int randomGenerator(int min, int max)
{
int random=0, trying=0;
int i=0;
trying = 1;
srand(clock());
while(trying)
{
random = (rand()/32767.0)*(max+1);
(random >= min) ? (trying = 0) : (trying = 1);
}
return (i++%2==0)?(random):(-1*random);
}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
This might be simple but I'm new to recursion in c. I want to find the sum of odd integers based on user's input. For example if user inputs 3, function returns 9 (1 + 3 + 5 = 9)
int recursiveSumNOdd(int n)
{
int start = -2; //later start = start+2, so it starts at 0
int n1 = n*2; //total number of digits with rec
int num = 0;
int sum=0;
int count=0;
if(start>=n1)
{
return 0;
}
else
{
start = start+2;
count++;
sum = sum +recursiveSumNOdd(start);
}
return sum;
}
Explanations in comment:
int recursiveSumNOdd(int n) {
if (n == 1 || n == 0)// first "if" in a recursive is its stop condition
return n;
return 2 * n - 1 + recursiveSumNOdd(n-1); // formula for 2->3, 3->5 etc
}
int main(void) {
printf("%d\n", recursiveSumNOdd(3));
return 0;
}
NB: You may want to handle integer overflow
NB2: You can have a mathematics formula to return instantly the result, it is way better, but I guess it was to understand better recursion?
return n * n; // the sum of odd numbers is the square of user's input
You are over-complicating things.
You cannot have the sum of negative elements.
int sum_n_odd(unsigned n)
{
What is the sum of 0 (zero) elements?
if (n == 0) return 0;
If you knew the sum of n - 1 numbers, what is the sum of n numbers?
return sum_n_odd(n - 1) + something; // something is easy to figure out
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Is this implementation of random number generation wrong?
int random_number(int l, int u)
{
int num;
num = rand()%u;
if (num<l && num>u)
{
while(num<l && num>u)
{
num = rand()%u;
}
}
return num;
}
This is not giving me the correct answer.
If I try random_number(4,8); it generates numbers like 0,1,2 etc.
Assuming u means upper and l means lower, then it is wrong. Try this:
int random_number(int l, int u) {
int num = rand() % (u - l);
return num + l;
}
Consider the lines.
int random_number(int l, int u)
num = rand()%u // result 0, 1, 3, .... 7
if (num<l && num>u)
random_number(4,8);
Code needs to follow the if() when num <4 and num > 8. An int cannot both be less than 4 and greater than 8 at the same time.
The usual idiom is
int random_number(int lower, int upper) {
int num;
num = rand()%(upper - lower + 1) + lower;
return num;
}
Extra code is needed to cope/detect upper < lower, upper - lower >= RAND_MAX
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 7 years ago.
Improve this question
Can someone tell what wrong with my code?
This code is attempting to deduce a palindrome for numbers from 100-999
#include <stdio.h>
int main ()
{
int temp, n, reverse = 0, rem;
n = 100;
while ( n <= 999)
{
temp = n;
while(temp!=0)
{
rem = temp%10;
reverse = reverse*10+rem;
temp/=10;
}
if ( reverse == n )
{
printf(" %d \t", n);
}
n = n + 1;
}
return 0;
}
Thanks.
You should be setting reverse back to 0 for each new n
while ( n <= 999)
{
reverse = 0; # <== otherwise you are using old value in reverse
temp = n;
while(temp!=0)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
How can I print the summation of 1 to 10 without using any loop, case, switch, goto.
To find the sum of a certain number of terms of an arithmetic sequence, use the formula Sn = n(a1 + an) / 2, where Sn is the sum of n terms (nth partial sum),
a1 is the first term, an is the nth term. So you can write:
int n = 10;
int somme = n * (n + 1) / 2;
printf("%d", somme);
Reference: http://www.regentsprep.org/regents/math/algtrig/atp2/arithseq.htm
Use recurrence: calculate sub result until you hit 0 and then return final result.
This is a solution using recursion. Pass the highest limit, the lowest limit, and 0 to SumNum, and it will print the sum of the numbers 1 through high. What it does is add the number high to sum, which you initialized to 0, then calls the function again with high - 1 for high:
void SumNum(int high, int low, int sum)
{
if (high == low) {
printf("%i\n", sum);
return;
}
else SumNum(high - 1, low, sum + high);
}
int main()
{
SumNum(10, 0, 0);
}
I am not sure if this is optimal C because I mainly use C++. I prefer the C++ version, where you just pass high and low from main() without passing the starting sum of 0 too:
void SumNum(int high, int low, int sum = 0)
{
if (high == low) {
printf("%i\n", sum);
return;
}
else SumNum(high - 1, low, sum + high);
}
int main()
{
SumNum(10,0);
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm having trouble with arrays. I copied this code from a book:
#include <stdio.h>
#include <stdbool.h>
int main (void)
{
int p, i, primes[50], primeIndex = 2;
bool isPrime;
primes[0] = 2;
primes[1] = 3;
for (p = 5; p <= 50; p = p + 2) {
isPrime = true;
for (i = 1; isPrime && p / primes[i] >= primes[i]; ++i)
if (p % primes[i] == 0)
isPrime = false;
if (isPrime == true) {
primes[primeIndex] = p;
++primeIndex;
}
}
for (i = 0; i < primeIndex; ++i)
printf ("%i ", primes[i]);
printf ("\n");
return 0;
}
In particular, I'm having trouble understanding the difference between the primeIndex and the i variables. The primeIndex refers to the array number and i refers to the number placed into the array. Right?
primeIndex is the place where the next found prime is written in the prime array, and also the number of primes known so far. i is the index of the prime used for trial division. For each candidate, i loops from 1 (we don't need to try out primes[0] = 2 because only odd numbers are checked) to the index of the first prime larger than the square root of the candidate.