What's needed in PL 511 --> 528 to program in C lang?

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Message
Author
User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#21 Post by technosaurus »

could you give a link to the tutorial you were following?

as for random numbers - random()
this should help (I use time for my seed number, otherwise you get the same seqence over and over because it defaults to seeding with 1)

Code: Select all

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int main(void){
srandom(time(NULL));
int r=random();
//printf("rand_int is %d\nRAND_MAX is %d\n",r,RAND_MAX);
printf("random int from 0-50 is %d\n",(int)(50 * (float)r /(float)RAND_MAX + 0.5));
}
notice the (int) and (float) ... those are used to cast a non-integer to an integer and a non-float to a float respectively ... not an easy thing to find, but essential to know ... if it wasn't srandom usage that caused non-random sequences, it was probably integer overflows in the math from not casting them

but as to the number series, that is mathematical logic which is better to figure out on your own (but here is a hint: use a random number to get the range and add it to the starting point)
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

User avatar
benny7440
Posts: 440
Joined: Mon 20 Apr 2009, 04:23

#22 Post by benny7440 »

Thanks, technosaurus, for responding with a good code to let me know how simple it really is!

Now I've to deal with output formatting but I'm not preocupied with it, I'm still in a debugging phase. Since I've devoted much time to this I'm very much at the rear of my way so, I think I'm going to come back to the code in about 2 days from now.

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#23 Post by technosaurus »

I made myself some macros, thought I'd share them with an example:

Code: Select all

#include <stdlib.h>
#include <stdio.h> //only for printf

#define RANDINT(r) ((int)(r * (float)random() /(float)RAND_MAX + 0.5))
#define OFFSETRANDINT(r,o) (RANDINT(r) + o)
#define RANDINTX2Y(x,y) (OFFSETRANDINT((y-x),x))

int main(){

srandom(time(NULL)); // important to call this first, but only once
printf("%d\n",RANDINT(2147483647)); //a random int between 0 and 2147483647
printf("%d\n",OFFSETRANDINT(10,10)); //starting @ 10 with a range of 10 (10-20)
printf("%d\n",RANDINTX2Y(0,10)); //between 0 and 10
}
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

User avatar
benny7440
Posts: 440
Joined: Mon 20 Apr 2009, 04:23

#24 Post by benny7440 »

Interesting set of 'functions'!

Are they defined somewhere in a library, & if so, which one?

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#25 Post by technosaurus »

they aren't "functions" per-se but they work like functions for all intents and purposes (they are referred to as macros) the function inside of the macros (random) is part of most if not all C libraries (same for srandom and printf)

the main difference with macros is that they basically get "inlined" by the preprocessor prior to compilation ... similar to what sed does with regular expressions to replace one text pattern with another

you can also do simple stuff like this to (commonly referred to as defines if they replace a variable instead of a function, but they work the same way)

#ifndef PI
#define PI 3.14
#endif

#ifndef PIXMAP_PATH
#define PIXMAP_PATH "/usr/share/pixmaps"
#endif
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

User avatar
benny7440
Posts: 440
Joined: Mon 20 Apr 2009, 04:23

#26 Post by benny7440 »

Thanks technosaurus for the data/explanations! It took me some days to re-read that stuff but, after a while, I think I've digested it (somewhat).

Right now I'm dealing with 'pointers' & I think it'll take me some days to make something useful with them. Until now I think a logical something isn't working for me. I haven't exhausted all the possibilities yet & this's why I'm not asking anything about them now. But it could happen in a few days...

Post Reply