Total noob here. Can someone give me an example on how i can generate a 2kHz sine wave array with white noise of variance 0.01 in C? This is what I have so far:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#define PI 3.141592653589793
int main() {
int i;
double buffer[10000];
for(i = 0; i < 10000; i++)
{
buffer[i] = sin(2000 * (2 * PI) * i / 6000) + sqrt(0.01)rand;
}
return 0;
}
(For reference)
You first have to seed the random-number generator using srand(), to which you should pass a unique value at every program-run.
Your code, modified for correctness:
#include <math.h>
#include <stdlib.h>
#include <time.h>
int main() {
srand((unsigned)time(NULL));
int i;
double buffer[10000];
for(i = 0; i < 10000; i++)
{
buffer[i] = sin(2000 * (2 * M_PI) * i / 6000) + sqrt(0.01) * rand();
}
/* ... */
return 0;
}
Related
This question already has answers here:
Why dividing two integers doesn't get a float? [duplicate]
(7 answers)
Closed 4 months ago.
here's the code it is supposed to display the value of my function (1/1+(25xx)) in a [-1,1] interval. But when I run it I have 1 as a result!!
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(){
int i,n;
double y=0;
double t=0;
double delta=0;
scanf("%d",&n);
delta=1/n;
for (i=0;i<n;i++)
{
t=t+delta;
y= 1 / (1 + (25*t*t));
printf("%lf \n",y);
}
return 0;}
Your program is working with integer math. Be sure to use double literals and to cast integers to doubles where needed.
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
int main() {
int i, n;
double y = 0, t = 0, delta;
scanf("%d",&n);
delta = 1 / (double)n;
for (i = 0; i < n; i++) {
t = t + delta;
y = 1.0 / (1.0 + (25.0 * t * t));
printf("%lf \n", y);
}
return 0;
}
Hello I need to create this summation and if you put the number 30000 the response should the number of pi, however it's not working here's the summation and here's the code.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <limits.h>
#include <ctype.h>
#include <stdbool.h>
int main( void ){
int num, k;
double pi= 0;
printf("Digite o total de termos >=30000: ");
scanf("%d", &num);
if (num < 30000){
printf("Erro.");
}else {
for (k = 1; k<= num; k++){
if (k % 2 == 0){
pi = (double)(-1)/ (2*k -1);
}else{
pi = (double)(1)/ (2*k -1);
}
pi = pi * 4;
}
printf("O valor de pi e %f", pi);
}
return 0; }
Here is a general way to sum things up:
double sum(int from, int to, double (*f)(int)) {
double ret = 0.0;
for(int i=from; i<to; i++)
ret+=f(i);
return ret;
}
And then you can write this function:
double fun(int current) {
double sign = current %2 == 0 ? -1.0 : 1.0;
return sign / (2*current - 1)
}
Finally, call it like this:
double pi = sum(1, num, fun);
Do note that this is probably not a good way to go if you're a beginner student that is looking for a solution to some homework.
#include <stdio.h>
int main(void) {
double pi=0;
for(int k=1; k<=3000; ++k)
{
pi += (2.*(k%2)-1) / (2*k-1); // This line does a summation, and uses floating point math (Not Integer Math)
}
printf("Pi : %f\n", 4*pi);
return 0;
}
I have tried this
Fix the range and also compute average and number of data greater than average
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int i;
float num;
srand(time(NULL));
FILE *fp;
fp = fopen("ran.dat", "w");
for (i = 0; i < 9; i++)
{
num = rand() % (-1.5,1.5);
fprintf(fp, "%f\n", num);
}
fclose(fp);
return 0;
}
You can generalize it by creating a function like so:
float random_float(float min, float max)
{
return ((float)rand() / ((float)RAND_MAX)) * (max - min) + min;
}
With both min and max inclusive.
This is the complete code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
float random_float(float min, float max)
{
return ((float)rand() / ((float)RAND_MAX)) * (max - min) + min;
}
int main()
{
srand(time(NULL));
FILE *fp;
fp = fopen("ran.dat", "w");
int i;
for (i = 0; i < 9; i++)
{
fprintf(fp, "%f\n", random_float(-1.5, 1.5));
}
fclose(fp);
return 0;
}
A few things I changed:
Declared the variable i immediately before the for loop. This is a good practice, declaring things only when you are about to use them;
Removed declaration of float num as it was unnecessary.
This will create 10 random numbers between -1.5 and 1.5.
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int main(){
int i;
float num;
srand(time(NULL));
FILE *fp;
fp = fopen("ran.dat","w");
for(i=0; i < 10; i++)
{
num = (double)rand()/RAND_MAX * 3.0 - 1.5;
fprintf(fp,"%f\n", num);
}
fclose(fp);
return 0;
}
I need in c code that generates two numbers in horizontally...so that i can get token numbers for my login system.
I need that i get this:
token=0.152644,0.429187
so in example i have token= and random generated numbers that have at beginning 0. and then 6 random generated numbers separated with , sign.
How to get get this in C?
I have try this code but it does not give me what i want_
#include <stdio.h>
#include <string.h>
typedef
union
{
char tmp[sizeof(unsigned long long)];
unsigned long long myll;
} ll_t;
unsigned long long llrand(void)
{
FILE *in=fopen("/dev/urandom", "r");
ll_t ll_u;
fread(ll_u.tmp, sizeof(ll_u.tmp), 1, in);
fclose(in);
return ll_u.myll;
}
int main()
{
char tmp1[64]={0x0};
char working[64]={0x0};
int i=0;
for(i=0; i< 1; i++)
{
while(strlen(tmp1) < 6)
{
sprintf(working, "%lu", llrand() );
strcat(tmp1, working);
}
tmp1[6]=0x0;
printf("%s\n", tmp1);
*tmp1=0x0;
}
return 0;
}
From output i get this:
747563
102595
Can code be simple and short?
You can use rand() function:
#include <stdio.h> /* printf, scanf, puts, NULL */
#include <stdlib.h> /* srand, rand */
#include <time.h> /* time */
int randomNumber(int min, int max)
{
/* generate secret number between min and max: */
int res = rand() % (max-min+1) + min;
return res;
}
int main()
{
int i = 0;
srand (time(NULL));
for (i=0; i<100; i++)
printf("%d ", randomNumber(10, 1000000));
return 0;
}
That is full detail for rand():
http://www.cplusplus.com/reference/cstdlib/rand/
Here is the code that is working perfect:
#include <stdio.h>
#include <stdlib.h>
int main() {
int n1, n2;
time_t t;
srand((unsigned) time(&t));
n1 = rand() % 1000000 + 1;
n2 = rand() % 1000000 + 1;
printf("token=0.%d,0.%d\n", n1, n2);
return 0;
}
And output is:
token=0.289384,0.930887
A propose a different approach. Instead of generating 2 numbers and format them into the output string, generate 12 different digits and put them directly in place.
srand(time(0));
char output[] = "taken=0.XXXXXX,0.YYYYYY";
for (int n = 0; n < 2; n++) {
for (int k = 0; k < 6; k++) {
output[9 * n + 8 + k] = rand() % 10 + '0';
// you might want to write a function that deals with rand() bias
}
}
puts(output);
This question already has answers here:
Why do I always get the same sequence of random numbers with rand()?
(12 answers)
Closed 9 years ago.
I have a function generating random numbers. Why does it always generate the same ones? I tried running the algorithm several times but always get the same results.
#ifndef UTIL_H
#define UTIL_H
#include <time.h>
#include <sys/time.h>
#include <stdlib.h>
#define MIN 0
#define MAX 100000
void randomArray (double *array, int length)
{
int i ;
for (i = 0; i < length; i++)
{
array[i] = (double) (rand () /
(((double) RAND_MAX + 1) / (double) (MAX - MIN + 1))) + MIN;
}
}
int main(void)
{
int i;
double test_array[9];
randomArray(test_array, 9);
for(i = 0; i < 9; i++)
printf("%f ", test_array[i]);
printf("\n");
return 0;
}
You need to seed the rand function. Use srand(time(NULL)) in the beginning of your main.
There are 3 problems in your code:
1) Add srand to your main() function:
int main(void) {
int i;
double test_array[9];
srand (time(NULL)); // here it is
randomArray(test_array, 9);
for(i = 0; i < 9; i++)
printf("%f ", test_array[i]);
printf("\n");
return 0;
}
2) Add an stdio.h library for using printf():
#include <stdio.h>
3) There is unterminated #ifndef that will couse an error when compiling.
Add #endif:
#ifndef UTIL_H
#define UTIL_H
#endif // here it is