LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-18-2017, 06:12 PM   #1
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
rand C prog and not being ran when called.


ok the basic stuff I got to get a random r g b a

Code:
typedef struct RandomColor
{
int r,g,b,a;

}RandomColor;

extern RandomColor rc;
function to call to get the random 0 - 255

Code:
void get_random_color(void)
{

rc.a = (rand() % 255);  
rc.g = (rand() % 255); 
rc.b = (rand() % 255); 
rc.a =255;
}
How I am testing this is with a cli program. Each time I execute it on the cli (so maybe that maybe way it is always the same I do not know, I just thought of that),
but I just have it calling that function each time then printf the values each time and it keeps coming up the same.

wait ....

Ok I just set it up to set a random color on the desktop while it is still running via a timer so that works.

it is just that the color will not change whenever I call for it get_random_color(); it gets a random color, sets it, the program exits,

run it again and i get the same color, always.

this is on the timer.

Code:
//   -c different image -rc random color     -D timer time  images located
//     each timer set    diff each timer set   set to 2 sec
$> ./mhsetroot -c         -rc -g 100x100       -D 2         /media/data/test
// -g 100x100  to make that image smaller so I can see color changes
// and be sure it still setting a different image each time. 

in trimmer set_random_colors
r 0 g 151 b 162 a 255

in trimmer set_random_colors
 
r 0 g 83 b 190 a 255
r = 0

red stays at zero as well there. why? anyone?

Last edited by BW-userx; 08-18-2017 at 06:18 PM.
 
Old 08-18-2017, 06:42 PM   #2
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,264
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Quote:
Originally Posted by BW-userx View Post
it is just that the color will not change whenever I call for it get_random_color(); it gets a random color, sets it, the program exits,

run it again and i get the same color, always.
From man 3 rand...

Code:
...rand() returns a pseudo-random integer in the range [0, RAND_MAX].  The seedp argument is
a  pointer to an unsigned int that is used to store state between calls.  If rand_r() is called with the
same initial value for the integer pointed to by seedp, and that value is not  modified  between  calls,
then the same pseudo-random sequence will result.
Quote:
Originally Posted by BW-userx View Post
red stays at zero as well there. why? anyone?
The reason for rc.r not being set should be obvious:

Code:
void get_random_color(void)
{
rc.a = (rand() % 255);  
rc.g = (rand() % 255); 
rc.b = (rand() % 255); 
rc.a =255;
}
Please make use of documentation and perform basic troubleshooting before posting.

Last edited by astrogeek; 08-18-2017 at 07:08 PM. Reason: formatting, changed ref to man page, added quote
 
Old 08-18-2017, 07:20 PM   #3
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Original Poster
Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
yeaaaaahhhhhh I think I got it figured out.
that is just the first set of randoms that get got on first time calling. so it is always going to be the same. ga duha

Code:
init_system_options r 0 g 0 b 0 a 0
OPTIONS: r 0 g 151 b 162 a 255
even calling twice before setting it I bet will get the same results .

mod:
@astrogeek
I just read yours after posting.

I'll just make a note to the program spit out info mess best used when cycling images with colors or just colors. (timer)

Last edited by BW-userx; 08-18-2017 at 07:24 PM.
 
Old 08-18-2017, 07:28 PM   #4
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Original Poster
Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
got any idea why r(ed) always stays 0
 
Old 08-18-2017, 07:59 PM   #5
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,264
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Quote:
Originally Posted by BW-userx View Post
got any idea why r(ed) always stays 0
Please read my previous post all the way through and look at your function, at the exact line where red is being set.

Last edited by astrogeek; 08-18-2017 at 08:21 PM.
 
1 members found this post helpful.
Old 08-19-2017, 07:43 AM   #6
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Original Poster
Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by astrogeek View Post
Please read my previous post all the way through and look at your function, at the exact line where red is being set.
yep: stupid drop down list pick one thinking you got the right one, but didn't (human) error.

I switched from greny to codeblocks and not use to this one yet. excuse or reason?

Last edited by BW-userx; 08-19-2017 at 07:53 AM.
 
1 members found this post helpful.
Old 08-20-2017, 11:17 PM   #7
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,264
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Quote:
Originally Posted by BW-userx View Post
yep: stupid drop down list pick one thinking you got the right one, but didn't (human) error.

I switched from greny to codeblocks and not use to this one yet. excuse or reason?
It has nothing to do with your editor or IDE, that is just another excuse.

It has to do with the fact that you continue to post without doing even basic troubleshooting of your own, before performing even basic search of the documentation, and continuing to ask others to troubleshoot your application code, rather than expending effort of your own.

Let us be very clear - had you once read the man page for rand(), or performed even a simple online search for "C rand() function", you could not have missed how that works. No excuses, please.

And had you even once actually looked at the single line in your code which assigns the value for your integer variable, you would not have asked why the value seems not to be assigned. And had you actually read my original post, you would not have asked that question twice.

This behavior must stop - now, not later.

You often claim to be a novice at C programming, running when you should be walking, and using LQ as a crutch. Yet you continue to attempt to write your applications without stopping to gain the deficient knowledge of the language you are using, often ignoring the best advice of others to not do so. Well, no more please.

First, it is obvious that you need a better understanding of basic use and troubleshooting of the C language.

There is a sticky thread at the top of the Programming forum, C/C++ Tutorials. It has many excellent tutorials and reference links for C/C++ - use it! It has a few bad links, but it has a wealth of information, online references, tutorials and free books. Read them, and understand! If you have difficulty understanding something about the language - this is the place to ask, after trying to figure it out yourself!

Here are a few from that list that you should start with:

Debugging C and C++ code in a Unix environment

Beej's Guide to C Programming

C Programming Tutorial

There are many more! Further excuses for not knowing the basics will be frowned upon.

Next, there are the LQ posting guidelines with which you should be familiar by now. Again I refer you to the Site FAQ and Welcome page, which are the standard guide to participation here at LQ. Please read them.

You have much to offer LQ, and LQ has much to offer you. But this continued pattern of behavior is harmful to LQ and not beneficial for yourself or others, and must end. Please make the effort, now.
 
Old 08-21-2017, 07:35 AM   #8
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Original Poster
Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by astrogeek View Post
It has nothing to do with your editor or IDE, that is just another ...
yes it did have something to do with my editor, and human behavior and patterns.

I type in r g b a

then I go to type in r g b a again, my brain is already set up for this pattern because of the first instance of behavior.

then when the human goes to repeat this same pattern of r g b a, and then is given a helping hand via some fancy drop down list that already knows what that person is talking about. but instead of giving this person the same patten that he is already using the drop down list changes the sequence of the same pattern, their for making room for error.


therefore YES it does have a LOT to do with the editor. because it changed the pattern sequence I was using.

therefore,

it is not an excuse it is Human Error on both parties, first and foremost,
by the ones that wrote the editor, because it changes the pattern that the user has already set, making room for error. As a result the user now has to put up his or her guard to check for this deviation of the pattern that the user has already set.

And the user, because he or she is not ready to accept or look for this change, or has his or hers guard down, the editor then is the leading cause because he or she has not looking for this deviation in the same patten that they have already set in order to make the needed adjustments to correct the error of the person that wrote the editor that causes the editor to change the sequence of the same said pattern in the first place.

so errors occur due to the first error, the editor, not the user.

that link under "excuse" that you posted to justify your judgment on me had to do with link list, this is a complete different situation.

you're letting your emotions cloud your judgments.

Last edited by BW-userx; 08-21-2017 at 08:02 AM.
 
Old 08-21-2017, 07:53 AM   #9
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,846

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
No, it is not the editor, but probably yes. If that editor knows exactly who is typing, what was entered and sometimes the letters are jumping/moving/disappearing just because the editor knows the human in front of the monitor actually looking out of the window .... to check the weather.
 
Old 08-21-2017, 08:11 AM   #10
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Original Poster
Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by pan64 View Post
No, it is not the editor, but probably yes.
contradiction really did you listen to yourself when you contradicted yourself in the same sentence?

now who is making some twisted example for an excuse of to try and justify keeping me in the wrong?
Quote:
If that editor knows exactly who is typing, what was entered and sometimes the letters are jumping/moving/disappearing just because the editor knows the human in front of the monitor actually looking out of the window .... to check the weather.
the moderator is never wrong. my butt. you and that other is no better then me. prone to error.

Last edited by BW-userx; 08-21-2017 at 08:18 AM.
 
Old 08-23-2017, 03:22 PM   #11
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,264
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Thread now closed.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Destructor called on objects in deque without it being called explicitly Snark1994 Programming 4 07-13-2011 08:05 AM
using rand in matlab lmvent Programming 10 02-23-2009 04:35 PM
I want to start a prog from another prog but not as child grupoapunte Programming 5 05-23-2005 05:37 PM
rand() question deiussum Programming 6 11-11-2004 02:10 PM
Need help using rand() in C KneeLess Programming 12 10-01-2003 12:51 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:23 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration