#include <time.h>
// double  RAND_MAX = 4294967296.0; //2147483647; //
void aleatorio();
void warmup_random(double);
void advance_random();
int flip(double);
double randomperc();
double aleatorios_0_1();
long  rnd(long, long);

double  Rseed;                               /* Random numbers seed */
double oldrand[55];                               /* Array of 55 random numbers */
int jrand;                                             /* current random number */


/* Initialize random numbers batch */
void aleatorio()
{
        int j1;
        time_t t;
        /*srand(time(0));*/
        Rseed = ((double) rand());
        /*printf( "\n \n %f ", Rseed);*/
        Rseed /= RAND_MAX;
        for(j1=0; j1<=54; j1++)
            oldrand[j1] = 0.0;
        jrand=0;
        warmup_random(Rseed);
}
/* Get random off and running */
void warmup_random(double random_seed)
{
    int j1, ii;
    double new_random, prev_random;

    oldrand[54] = random_seed;
    new_random = 0.000000001;
    prev_random = random_seed;

    for(j1 = 1 ; j1 <= 54; j1++){
        ii = (21*j1)%54;
        oldrand[ii] = new_random;
        new_random = prev_random-new_random;
        if(new_random<0.0) new_random = new_random + 1.0;
            prev_random = oldrand[ii];
        }
        advance_random();
        advance_random();
        advance_random();
        jrand = 0;

}

/* Create next batch of 55 random numbers */
void advance_random()
{
    int j1;
    double new_random;

    for(j1 = 0; j1 < 24; j1++){
        new_random = oldrand[j1] - oldrand[j1+31];
        if(new_random < 0.0) new_random = new_random + 1.0;
        oldrand[j1] = new_random;
    }
    for(j1 = 24; j1 < 55; j1++){
        new_random = oldrand [j1] - oldrand [j1-24];
        if(new_random < 0.0) new_random = new_random + 1.0;
        oldrand[j1] = new_random;
    }
}

/* Flip a biased coin - true if heads */
int flip(double prob){
    double randomperc();
    if(randomperc() <= prob)
        return(1);
    else
        return(0);
}

/* Fetch a single random number between 0.0 and 1.0 - Subtractive Method */
/* See Knuth, D. (1969), v. 2 for details */
/* name changed from random() to avoid library conflicts on some machines*/
double randomperc()
{
    jrand++;
    if(jrand >= 55){
        jrand = 1;
        advance_random();
    }
    return((double) oldrand[jrand]);
}


double aleatorios_0_1(){
    int i;
    double x;
    /* inicializacion de la semilla de aleatorios */
    srand(time(0));
    /* aleatorios % 0 y 1 */
    for(i=0; i<10; i++){
        x =(double) (((rand()%32000) / 32000.0)-1);
           /*   printf("  %f     ", x);*/
    }
    return x;
 }

 /* Pick a random integer between low and high */
 long rnd(long low, long high){
    long i;
    double randomperc();

    if(low >= high)
    i = low;
    else {
    i = (randomperc() * (high - low + 1)) + low;
    if(i > high) i = high;
    }
    return(i);
}



c:\users\jana\documents\visual studio 2008\projects\misa\aleatori.c(22) : warning C4013: 'rand' undefined; assuming extern returning int
c:\users\jana\documents\visual studio 2008\projects\misa\aleatori.c(24) : error C2065: 'RAND_MAX' : undeclared identifier
c:\users\jana\documents\visual studio 2008\projects\misa\aleatori.c(99) : warning C4013: 'srand' undefined; assuming extern returning int
c:\users\jana\documents\visual studio 2008\projects\misa\aleatori.c(116) : warning C4244: '=' : conversion from 'double' to 'long', possible loss of data
Build log was saved at "file://c:\Users\jana\Documents\Visual Studio 2008\Projects\misa\misa\Debug\BuildLog.htm"
misa - 1 error(s), 3 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

this is a subprogram from my work

Just by looking at your errors :

You forgot to include proper library. Fastest way is to
#include<iostream> using namespace std; Although its not good
practice.

when i add #include<iostream> no. of errors increase to102 error

this program is apart of header file
this is a part of it


#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <malloc.h>
#include "aleatori.c" // this is the above program

#define tam_memoria 100
#define cadena 820
#define objetivos 5
#define POB 60
#define num_var 32
#define nsf 100

Use code tags - code is unreadable without code tags and formatting/indentation.

> #include<iostream> using namespace std; Although its not good
Except it appears to be a C program from the logs.

But then again, who knows, the whole mess was posted without code tags to begin with. Don't people READ intro threads? Of course not, that would get in the way of being nice to readers.

> warning C4013: 'rand' undefined; assuming extern returning int
You're missing stdlib.h

And some others from the look of things.

The number of errors you have in code doesn't dictate how close you are to finishing. It can go from 1 error -> 12 errors -> working.

OK, well the error messages say it all...
For starters you need to uncomment the definition of RAND_MAX which is the cause of your error.

Secondly, as Salem has pointed out, you need to include stdlib.h, which will get rid of the warnings about rand and srand.

Finally line 116:

i = (randomperc() * (high - low + 1)) + low;

The variable 'i' has been declared as a long, as have the variables 'high' and 'low'. The randomperc function returns a double, which is the cause of the final warning, basically it's telling you that the double will automatically be converted to long and that it could result in a loss of precision (due to the inherent floating point issues.)
Being a warning, you could just ignore it, but it could be worth explicitly casting the value returned by randomperc to long.
i.e.

i = ( ((long)randomperc()) * (high - low + 1)) + low;

Which will stop the warning from occurring.

Cheers for now,
Jas.

when i include stdlib.h the errors become

c:\users\jana\documents\visual studio 2008\projects\misa\aleatori.c(20) : warning C4101: 't' : unreferenced local variable
c:\users\jana\documents\visual studio 2008\projects\misa\aleatori.c(99) : warning C4244: 'function' : conversion from 'time_t' to 'unsigned int', possible loss of data
c:\users\jana\documents\visual studio 2008\projects\misa\aleatori.c(121) : error C2857: '#include' statement specified with the /Ycvariable.h command-line option was not found in the source file
Build log was saved at "file://c:\Users\jana\Documents\Visual Studio 2008\Projects\misa\misa\Debug\BuildLog.htm"
misa - 1 error(s), 2 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

when i include stdlib.h the errors become and with some modifications the error become


c:\users\jana\documents\visual studio 2008\projects\misa\aleatori.c(121) : error C2857: '#include' statement specified with the /Ycvariable.h command-line option was not found in the source file
Build log was saved at "file://c:\Users\jana\Documents\Visual Studio 2008\Projects\misa\misa\Debug\BuildLog.htm"
misa - 1 error(s), 0 warning(s)
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========

Sounds like the crappy PCH strikes again.


Try turning PCH off.
It's only relevant if you're compiling vast projects which are pretty stable.

Sounds like the crappy PCH strikes again.
[attach]11218[/attach]

Try turning PCH off.
It's only relevant if you're compiling vast projects which are pretty stable.

it try it but it increase errors

I've no idea then.

Did you start with an empty Win32 console project?

I've no idea then.

Did you start with an empty Win32 console project?

yes

Post your whole current code

dear all
i made some changes in program properties so i recieve this

c:\users\jana\documents\visual studio 2008\projects\misa\variable.h(3) : fatal error C1083: Cannot open include file: 'cmath.h': No such file or directory
Build log was saved at "file://c:\Users\jana\Documents\Visual Studio 2008\Projects\misa\misa\Debug\BuildLog.htm"
misa - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

please my Phd first step is to run this program
please help me
thanks for help

when execute Misa.exe this message appeasrs to me
Debug Assertion Failed!

Debug assertions usually show up when you fail to do proper error checking.

Eg.

FILE *fp = fopen( "missing_file.txt", "r" );
// no check for success
int ch = fgetc( fp );  // debug assert here

this is the message i receive

Debug Assertion Failed!

Program:................misa.exe
File:f:\dd\vctools\crt_bld\self_x86\crt\src\fgetc.c
line: 41

Expression: (stream !=NULL)

for information on how your program can cause an assertion failer,see the Visual C++ documentation on asserts.

(press Retry to debug the application )


Abort retry ignore

File:f:\dd\vctools\crt_bld\self_x86\crt\src\fgetc.c
Expression: (stream !=NULL)

This is an easy one. The stream you passed to fgetc is NULL. It means fopen returned NULL because the file could not be opened for some reason. Check the result before using it:

FILE* fp = fopen(file, mode);

if (!fp)
{
    perror("fopen");
    exit(EXIT_FAILURE);
}

Replace exit() with whatever recovery method you want. :)

thanks all for help me the program works very gooooooooood

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.