rand()%28 only generating multiples of 7 - c

This code of C generates only multiples of 7 in Xcode:
#include <stdlib.h>
#include <time.h>
int main()
{
srand(time(NULL));
printf("%d", rand()%28);
}
It will also generate only multiples of 7 with multiples of 7 put in place of 28 (rand()%56 for example does the same thing). Any idea what's causing this problem? Thanks a lot for your help!

Related

Random sequence looks not random in c

I'm implementing a basic program which generate 2 random number. The problem is the result of first number looks like it is following some kind of pattern, but the second still looks right.
Output:
6584 679
6587 1427
6591 9410
6594 156
7733 3032
7737 3780
This is my code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(){
srand(time(NULL));
int a = rand()%10001, b= rand()%10001;
printf("%d %d", a,b);
return 0;
}
So what is the problem here and how to fix it.
Any help would be appreciated.
I am using windows 10 64 bits, gcc 8.1.0.
time(NULL) value acts as the same seed value every time you run the program. The reason behind that your CPU will generate a similar starting NULL time for every time. To get rid of this kind of effect you need to play with the seed value, so that even if your computer starts with the same time(NULL) value, it needs to get seed different than the other runs. For that purpose, you can simply do the following:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
int main(){
srand((unsigned)time(NULL) * (unsigned)getpid());
int a = rand()%10001, b= rand()%10001;
printf("%d %d", a,b);
return 0;
}
Full credit to #pmg, thanks for the comments to improve the solution.

Memory Allocation Error with C? Code works when unused variable is defined; crashes otherwise

I'm a novice C programmer and am completely stuck on debugging a program that relies on the CFITSIO package [sorry in advance if I get the terminology wrong below]. I have two fits that I am reading in: 1. "BFmask.fits", 2. "DFmask.fits". These are two files that either have a 1 or a 0 as elements.
These are defined as double precision pointers as below:
double *maskdark,*maskbright;
Memory allocation is as follows:
maskdark=malloc(size*sizeof(double)); //dark mask
maskbright=malloc(size*sizeof(double)); //bright mask
where "size" is a long variable with a value of 490000
THE PROBLEM: When I execute the program, it apparently does not properly read in BFmask.fits or DFmask.fits (i.e. does not properly read in entries for maskbright or maskdark).
HOWEVER, when I declare a third variable called 'mask' (i.e. double *mask;), then the program DOES execute properly.
What is the cause and solution to this problem?
How do I avoid similar ones in the future?
I have checked dozens of times to make sure this variable is not used anywhere in the program. This makes no sense to me.
#include <stdio.h>
//For declaration of open, close
#include <sys/stat.h>
#include <fcntl.h>
#include <math.h>
#include <unistd.h>
#include <time.h>
//GSL Matrix STuff
#include <gsl/gsl_vector.h>
#include <gsl/gsl_math.h>
#include <gsl/gsl_matrix.h>
#include <gsl/gsl_eigen.h>
#include <gsl/gsl_blas.h>
//CFITSIO
#include <fitsio.h>
int main ()
{
long sizetotdm,cmwfsbrightindex,cmwfsdarkindex;
long size,sizex,sizey;
double *maskdark,*maskbright;
//double *mask; //if I uncomment this, then the program works properly somehow
fitsfile *fptr;
float nullval;
int status, anynull;
cmwfsbrightindex=0;
cmwfsdarkindex=0;
sizex=700; sizey=700; size=sizex*sizey;
/* Define Masks for Dark Field and Bright Field */
maskdark=malloc(size*sizeof(double)); //dark mask
maskbright=malloc(size*sizeof(double)); //bright mask
/* Read in Dark and Bright Field Mask files */
sprintf(s_file,"./06_21_19_mask/BFmask.fits");
fits_open_file(&fptr,s_file,READONLY,&status);
fits_read_img(fptr,TDOUBLE,1,size,&nullval,maskbright,&anynull,&status);
sprintf(s_file,"./06_21_19_mask/DFmask.fits");
fits_open_file(&fptr,s_file,READONLY,&status);
fits_read_img(fptr,TDOUBLE,1,size,&nullval,maskdark,&anynull,&status);
for (i=0;i<size; i++)
{
if (*(maskbright+i)==1)
{
cmwfsbrightindex+=1;
}
if (*(maskdark+i)==1)
{
cmwfsdarkindex+=1;
}
}
printf("the number of pixels is %ld %ld \n",cmwfsbrightindex,cmwfsdarkindex);
}
Results should be ...
the number of pixels is 24212 24952 //this happens when I define an unused variable
The results are ...
the number of pixels is 0 0 //this happens when I comment out the unused variable

wchar_t strings not working C

I'm trying to create a program that needs to get string input from the user, since it's has to work with portuguese words i'm using wchar_t,the problem is,
C seems to have a will, because when i need it to work it doens't but then out of the blue it works in some simple test.
the following code worked 5 minutes ago and now it doesn't:
#include <stdio.h>
#include <wchar.h>
#include <locale.h>
/
int main(){
setlocale(LC_ALL,"Portuguese");
wchar_t meu[3];
fgetws(meu,3,stdin);
fputws(meu,stdout);
return 0;
}

C++ vector.assign (contents of char array) works in WinXP-32, fails in Win10-64; why?

On this StackExchange topic, first answer and last comment upon that answer, I learned to assign the contents of my char array (which I just read from file) into my vector. The following code works fine in Windows XP, 32-bit, Visual Studio 2010, but fails in Win10, 64-bit, Studio 2012. Both projects use the Unicode character set. The contents of the file myConfig.txt are (separated by tabs):
words 3 mobius lagrange gauss
I am a complete noob, so if some mistake seems too stupid for anyone to make, go ahead and assume I made it.
The code:
#include "stdafx.h"
#include <windows.h>
#include <vector>
#include <iterator>
#include <string.h>
#include <wchar.h>
#include <tchar.h>
using namespace std;
vector<wchar_t> wvec;
int n;
wchar_t ss[256];
FILE* pfile;
int _tmain(int argc, _TCHAR* argv[])
{
fopen_s(&pfile,"myConfig.txt","r");
fwscanf_s(pfile,L"%ls",&ss);
wprintf(L"var name is %ls\n",ss);
fwscanf_s(pfile,L"%d",&n);
printf("num words is %d\n",n);
for (int i=0; i<3; i++) {
fwscanf_s(pfile,L"%ls",&ss);
wvec.clear();
n=wcslen(ss);
wprintf(L"vec empty %ls length %d\n",vec,vec.size());
wvec.assign(ss,ss+n+1); // +1 to contain null char
wprintf(L"vec sz %d filled %ls\n",wvec.size(),wvec);
}
printf("press Enter to finish\n");
getchar();
return 0;
}
On the Win10 machine, the output says, in part, "vec sz 7 filled ???", when I run/debug from the development environment. When I run the exe in the x64\Release folder, the corresponding line of output says "vec sz 5 filled ???", while the contents of myConfig.txt are exactly the same.
On the XP machine, the output is perfect.
Finally answered my own question: RTFM. It should not be
fscanf_s(pfile,"%s",&s);
It should be
fscanf_s(pfile,"%s",s, _countof(s));
I dunno why it worked in XP (convenient #bytes/char?) but, whatever.

C-Checking if input (float) is purely integer or float

I want to check if the user input is purely integer or a float. I attempted to do this by using floor and ceilfand comparing the values to the original x value in a function. However, this seems to be a tad bit problematic as the function keeps returning 0 instead of 1 for certain numbers like 5.5, when floor(5.5)!=5.5 and ceilf(5.5)!=5.5. This is my code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <conio.h>
#include <stdbool.h>
int intchecker(float x)//in a separate file
{
if (floor(x)==x && ceilf(x)==x)
{
//printf("%f",floor(x));
return 0;
}
else {
return 1;
}
}
int main()
{
char line[] = " +----+----+----+----+----+----+----+----+----+----+---+";
char numbers[] = " 0 5 10 15 20 25 30 35 40 45 50";
float balls,slots;
int slot[9];
printf("==========================================================\nGalton Box Simulation Machine\n==========================================================\n");
printf("Enter the number of balls [5-100]: ");
scanf("%f",& balls);
if (balls>100 || balls<5){
printf("/nInput is not within the range. Please try again.");
}
else if (intchecker(balls)==1){
printf("/nInput is not an integer. Please try again.");
}
else {
printf(" This is an integer.");
//some more code here
}
}
I tried placing just the intchecker code in another project, which seems to work fine without any bugs, unlike in the previous project, where when I used a printf statement to check if the floor(x) value was correct, it kept showing different answers, e.g. "-2.000000" when input was 5.2. This is my code for the second project:
#include <stdio.h>
#include <stdlib.h>
#include<math.h>
int main()
{
float x;
scanf("%f",&x);
if (floor(x)==x && ceilf(x)==x){
printf("Integer");
return 0;
}
else {
printf("Non-Integer");
return 1;
}
}
How is it possible that the second code works perfectly when the first code does not? Is something wrong with my way of writing/calling the function?(I am relatively new to functions-only 2 weeks of exposure so far)
I searched online and have seen many answers to check if input is integer or float, even on stackoverflow.com itself, but my wish is not to find out other ways to check if input is integer or float (for if I wished to do that, I could just google it, and there are many such questions on stackoverflow.com as well), but to comprehend why my first code does not work, for, as far as I know, it ought to work well without any of the bugs it is currently facing.
Any help is greatly appreciated!:)
Assuming a missing function declaration:
main.c is missing the prototype for int intchecker(float x) so main.c assumes the old-school prototype of int intchecker(int x) and the code exhibits undefined behavior. Anything could happen.
Add prototype in main.c or put it in separate.h and include that header file here and in separate.c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <conio.h>
#include <stdbool.h>
int intchecker(float x);
int main(void) {
...

Resources