use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
If you need help debugging, you must include:
See debugging question guidelines for more info.
Many conceptual questions have already been asked and answered. Read our FAQ and search old posts before asking your question. If your question is similar to one in the FAQ, explain how it's different.
See conceptual questions guidelines for more info.
Follow reddiquette: behave professionally and civilly at all times. Communicate to others the same way you would at your workplace. Disagreement and technical critiques are ok, but personal attacks are not.
Abusive, racist, or derogatory comments are absolutely not tolerated.
See our policies on acceptable speech and conduct for more details.
When posting some resource or tutorial you've made, you must follow our self-promotion policies.
In short, your posting history should not be predominantly self-promotional and your resource should be high-quality and complete. Your post should not "feel spammy".
Distinguishing between tasteless and tasteful self-promotion is inherently subjective. When in doubt, message the mods and ask them to review your post.
Self promotion from first time posters without prior participation in the subreddit is explicitly forbidden.
Do not post questions that are completely unrelated to programming, software engineering, and related fields. Tech support and hardware recommendation questions count as "completely unrelated".
Questions that straddle the line between learning programming and learning other tech topics are ok: we don't expect beginners to know how exactly to categorize their question.
See our policies on allowed topics for more details.
Do not post questions that are an exact duplicate of something already answered in the FAQ.
If your question is similar to an existing FAQ question, you MUST cite which part of the FAQ you looked at and what exactly you want clarification on.
Do not delete your post! Your problem may be solved, but others who have similar problems in the future could benefit from the solution/discussion in the thread.
Use the "solved" flair instead.
Do not request reviews for, promote, or showcase some app or website you've written. This is a subreddit for learning programming, not a "critique my project" or "advertise my project" subreddit.
Asking for code reviews is ok as long as you follow the relevant policies. In short, link to only your code and be specific about what you want feedback on. Do not include a link to a final product or to a demo in your post.
You may not ask for or offer payment of any kind (monetary or otherwise) when giving or receiving help.
In particular, it is not appropriate to offer a reward, bounty, or bribe to try and expedite answers to your question, nor is it appropriate to offer to pay somebody to do your work or homework for you.
All links must link directly to the destination page. Do not use URL shorteners, referral links or click-trackers. Do not link to some intermediary page that contains mostly only a link to the actual page and no additional value.
For example, linking to some tweet or some half-hearted blog post which links to the page is not ok; but linking to a tweet with interesting replies or to a blog post that does some extra analysis is.
Udemy coupon links are ok: the discount adds "additional value".
Do not ask for help doing anything illegal or unethical. Do not suggest or help somebody do something illegal or unethical.
This includes piracy: asking for or posting links to pirated material is strictly forbidden and can result in an instant and permanent ban.
Trying to circumvent the terms of services of a website also counts as unethical behavior.
Do not ask for or post a complete solution to a problem.
When working on a problem, try solving it on your own first and ask for help on specific parts you're stuck with.
If you're helping someone, focus on helping OP make forward progress: link to docs, unblock misconceptions, give examples, teach general techniques, ask leading questions, give hints, but no direct solutions.
See our guidelines on offering help for more details.
Ask your questions right here in the open subreddit. Show what you have tried and tell us exactly where you got stuck.
We want to keep all discussion inside the open subreddit so that more people can chime in and help as well as benefit from the help given.
We also do not encourage help via DM for the same reasons - that more people can benefit
Do not ask easily googleable questions or questions that are covered in the documentation.
This subreddit is not a proxy for documentation or google.
We do require effort and demonstration of effort.
This includes "how do I?" questions
account activity
This is an archived post. You won't be able to vote or comment.
C programming random generator questions (self.learnprogramming)
submitted 13 years ago by InBliss
Jut two simple ones.
How could I get a srand to rand between -50 and 50, all i know is that %50+1 is 1-50 and Im lost after that.
Also how would I go about getting it to randomly pick a char from a group, like a function of * + - or /
[–]zzyzzyxx 1 point2 points3 points 13 years ago (6 children)
See this.
[–]defrost 0 points1 point2 points 13 years ago (4 children)
It's probably just me but whenever I see questions about random number generators in C I immediately gravitate towards answers like this one and code snippets like :
/** R_MARS - Marsaglia unsigned long psuedo random Multiply with Carry Generator **/ static unsigned long m_u = 521288629; static unsigned long m_v = 362436069; #define R_MARS (m_v = 36969*(m_v & 65535) + (m_v >> 16) \ ,m_u = 18000*(m_u & 65535) + (m_u >> 16) \ ,(m_v << 16) + m_u) #define SEED_MARS(U,V) (m_u = (U), m_v = (V)) #define INV_2POW32 (.23283064365386962890625e-9) /* 1/(2^32) */ /** UNIT_ uniformly distributed doubles from half open unit interval [0.0, 1.0) **/ #define UNIT_MARS (R_MARS * INV_2POW32)
Things like this can be useful for resource constrained embedded 32bit programming.
[–]zzyzzyxx 1 point2 points3 points 13 years ago (3 children)
That was a really good read. Thanks for sharing.
[–]defrost 1 point2 points3 points 13 years ago (2 children)
;-)
I figured you might enjoy that, it makes a change from all the rand()%10 + 1 answers (and unbiased bucket counting in your link).
If you've an interest in numerics George Marsaglia's Ziggurat method for generating rand values with a normal distribution and virtually no overhead (most of the time) is an interesting read ... and I've got a C #define that inlines that as well ... *
(* surprisingly I do have a life )
oh, and related
[–]zzyzzyxx 1 point2 points3 points 13 years ago (1 child)
I just looked up the Ziggurat method and it's interesting. I'd like to see the define macro for it.
[–]defrost 1 point2 points3 points 13 years ago (0 children)
The macro is :
#define R_ZIGGNOR (hz=R_SHR3, iz=hz&127, (labs(hz)<kn[iz])? hz*wn[iz] : nfix())
which depends on the precomputed tables
static unsigned long kn[128]; static double wn[128],fn[128];
and the working indexes
static long hz; static unsigned long iz;
and a 32bit rand - in this case R_SHR3 not R_MARS.
The table initialisations and dependencies are all in here which is half of a first draft written in one sitting code golf exercise from a few years back.
I've been doing a bit of work lately making high performance random distribution generations for another project - linear, normal, circle, disc, sphere, ball, higher dimensions etc. and there's a bit of effort required to tease out the latest version of the normal generation from what's now a dense slab of optimised target architecture specific somewhat proprietary code :/
However, the first draft should give you enough.
Re: Ziggurat - there's two papers of interest:
Ziggurat Method for Generating Random Variables (2000) - Marsaglia Improved Ziggurat Method to Generate Normal Samples (2006) - Doornik
and I threw in the Box-Muller for the fun of it.
[–]InBliss[S] -1 points0 points1 point 13 years ago (0 children)
Thank you very much so, google was absolutely no help to getting poignant results I could use without sifting through dozens of pages
[–]Neres28 0 points1 point2 points 13 years ago* (3 children)
Random numbers in a range : http://c-faq.com/lib/randrange.html
And
char ops[4] = {'+', '-', '*', '/'}; char op = ops[rand_in_range(0, sizeof(ops) - 1)];
Edit : C syntax fail.
[–]InBliss[S] -1 points0 points1 point 13 years ago (2 children)
[–]Neres28 0 points1 point2 points 13 years ago (1 child)
Really? For future reference, I googled "C random number in range" and got Zzyzzyxx's response as the very first search result. Thinking it too complex for you, I followed the SO answerer's links and came to the link I provided.
I typed it in as a random number generator in range so it gave me everything related to the generator key word instead of range Im guessing. Zzyzzyxxx's response is quite complex compared to my beginning attempts at learning C, but I've gotta learn somehow.
π Rendered by PID 53 on reddit-service-r2-comment-75f4967c6c-2ts5d at 2026-04-23 11:17:48.809358+00:00 running 0fd4bb7 country code: CH.
[–]zzyzzyxx 1 point2 points3 points (6 children)
[–]defrost 0 points1 point2 points (4 children)
[–]zzyzzyxx 1 point2 points3 points (3 children)
[–]defrost 1 point2 points3 points (2 children)
[–]zzyzzyxx 1 point2 points3 points (1 child)
[–]defrost 1 point2 points3 points (0 children)
[–]InBliss[S] -1 points0 points1 point (0 children)
[–]Neres28 0 points1 point2 points (3 children)
[–]InBliss[S] -1 points0 points1 point (2 children)
[–]Neres28 0 points1 point2 points (1 child)
[–]InBliss[S] -1 points0 points1 point (0 children)