Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 50 51 [52] 53 54 ... 91

Author Topic: Programming Help Thread (For Dummies)  (Read 100787 times)

anzki4

  • Bay Watcher
  • On the wings of maybe
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #765 on: September 20, 2012, 02:14:24 am »

EDIT: How do you define a range for rand()? Example; if I wanted to generate a number between 1 and 100, what would I do?
Code: [Select]
int r = rand() % max + min;So between 1 and 100 would be
Code: [Select]
int r = rand() % 100 + 1;
Logged

Shades

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #766 on: September 20, 2012, 03:03:57 am »

EDIT: How do you define a range for rand()? Example; if I wanted to generate a number between 1 and 100, what would I do?

rand() returns an integer between 0 and RAND_MAX. Oafter that you can do what you like with it to get your range. The most common thing is to use modulus (%) like anzki4 suggested.

Code: [Select]
int r = rand() % max + min;

This is true if your min is 1, however for anything else you have to reduce the random range before adding the minimum.

Code: [Select]
int r = rand() % (max - min + 1) + min;
Logged
Its like playing god with sentient legos. - They Got Leader
[Dwarf Fortress] plays like a dizzyingly complex hybrid of Dungeon Keeper and The Sims, if all your little people were manic-depressive alcoholics. - tv tropes
You don't use science to show that you're right, you use science to become right. - xkcd

Chattox

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #767 on: September 20, 2012, 06:04:26 am »

Thanks, that really helps :)

Next question: Why would you use a do/while loop instead of a for loop? They both ensure that the statement will run at least once, provided you don't mess up the for loop.

(Also, if there's like, a newbie programmer forum where I could ask my questions without clogging up this thread with my noobishness, please point me to it xD)

EDIT: DERP nevermind, I just realised how stupid that question is. Disregard xD
« Last Edit: September 20, 2012, 06:09:01 am by Chattox »
Logged
"10 z levels down, 10 tiles north is some blood, i shall go clean it before it drives me to insanity with it's crimson color"
The setting of Half-Life 2 Episode 3's release: "It is the 41st Millennium. For more than a hundred centuries the Gabe has sat immobile on the..."

Valid_Dark

  • Bay Watcher
  • If you wont let me Dream, I wont let you sleep.
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #768 on: September 20, 2012, 06:14:39 am »

thats what this thread is for, feel free to ask away,

and I'm going to answer your question anyways,

for loops are for when you know exactly how many times you want it to loop,
while, do while and while loops run until the condition is met.

Quote
while, do while and while
lol :P
Logged
There are 10 types of people in this world. Those that understand binary and those that don't


Quote
My milkshake brings all the criminals to justice.

lorb

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #769 on: September 20, 2012, 08:07:30 am »

EDIT: How do you define a range for rand()? Example; if I wanted to generate a number between 1 and 100, what would I do?

rand() returns an integer between 0 and RAND_MAX. Oafter that you can do what you like with it to get your range. The most common thing is to use modulus (%) like anzki4 suggested.

Yes, it's very common and it's also plain wrong. Don't do the modulus thing as it introduces a bias (except if your range evenly divides RAND_MAX) Use this instead if you want a fast solution that is not awfully biased:
int r = min + rand() / ( RAND_MAX / ( max - min ) + 1 );

If you really care about the randomness of your numbers read more about it here. It also tells you why the modulo thing is just awfully wrong and the division variant only slightly wrong and also proposes better ways to get random numbers.
Logged
Please be gracious in judging my english. (I am not a native speaker/writer.)
"This tile is supported by that wall."

Starver

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #770 on: September 20, 2012, 09:23:12 am »

...also, unless I'm mistaking things, while selecting for a local min and max of 1 and 100 produces an initial 0..99 (from the modulus) which becomes 1..100 after the addition of the minimum value, if you had a min and max of 50 and 100, that would give you 50..149 as a final range, not what you want at all.

(Ignoring, for now, the modulus bias as already mentioned[1], you need to mod by the range (max-min+1) to get 0..range-1, then add the value of min.  This sends the lower end to 0+min, which is min (as you want), and the upperend goes from range-1 (or (max-min+1)-1 to (max-min+1)-1+min, which comes out as plain "max".

When looking at "int r = min + rand() / ( RAND_MAX / ( max - min ) + 1 );", this looks roughly the same, but I'm sure there's a value of one missing in there.  I'd want to check if rand() actually gives a RAND_MIN effective value of zero or one, and whether RAND_MAX is actually the top value, or whether that's the value above.  I'm assuming we're talking C-family code here (sorry, missed any specifics on that), but not all language's versions of rand() behave in that same way.

Spoiler: An example from Perl (click to show/hide)


((Oh, and that's all assuming "between the values m and n" in the OQ is also qualified by the word "inclusively".  The trouble with a lot of discussions about PRNG ranges is the "pass it a maximum and it'll give you a number between (0|1, depending on implementation) and that maximum"...  Does it actually give you that maximum?  I'd be tempted to run a quick trial program to give me a running total of a million random numbers from the equivalent of "rand(10)" and see both if 10 is included in the end tota (and, if so, whether it's roughly in proportion to the other digits) and also whether zero is encountered (it'll probably be in proportion to all numbers <10, if it is, but again you can check that).

Or check your MyRand() function directly (you might as well functionise it, so that you don't have to worry about typing the maths right every single time), so you make sure you've tuned it to your own expectations.  Define an array that you know has indexes 0..10, plus an underflow and overflow counter (or another storage structure that will accept such information), all values initially zero, run your "MyRand(1,10)" a million times and each time increment the result, or the underflow/overflow variables if it's not a number that's inclusively between one and ten.  Then print the final tallies and see if they look Ok.  Missing numbers (or zero present when you didn't expect it), any further underflow/overflow counts (you could have had a "minimumseen" and "maximumseen" instead, or as well, to help you tie down the true range of your function) and any obvious imbalance in those that you do get from the standard recorded set means you need to look at the maths surrounding the rand(), and (if applicable) within its parameter.  !!Science!!  Probably good practice.))


[1] Probably trivial for trivial usages of numbers that are several orders of magnitude lower than the maximum unprocessed random number, which is what you get if looking for values 1..100 on a system with a 16/32-bit top end Rand...

Logged

Chattox

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #771 on: September 25, 2012, 04:52:17 pm »

I don't get how pointers make anything better. Can someone explain?
Logged
"10 z levels down, 10 tiles north is some blood, i shall go clean it before it drives me to insanity with it's crimson color"
The setting of Half-Life 2 Episode 3's release: "It is the 41st Millennium. For more than a hundred centuries the Gabe has sat immobile on the..."

Thendash

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #772 on: September 25, 2012, 06:10:50 pm »

A pointer holds the actual address in the memory instead of just referencing it. So if you change a value through a pointer then any variable that references that address will change as well.
Code: (Written in C) [Select]
//a integer variable
int x;
//a pointer that can hold the address of type int. The * is used when declaring the pointer or when you want to reference the value and not the address. It's called the dereferencing operator
int *ptr;

x = 1;
//the & means "address of" and returns the address of the variable instead of the value. So now ptr points to the address that x uses
ptr = &x;

//print the value of x, it would show 1
printf(x);
//use * to change the value that ptr is point to
*ptr = 100;

//print the value of x again, this time it would show 100 because you changed the value using ptr
printf(x); //it would return 100;
There is a lot more to pointers than this though. This is just a basic explanation of the idea behind pointers. I recommend looking up some tutorials on pointers for your specific language as pointers are handled a little differently in each language.
Logged
Thendash's Livestream

This game could honestly give out hand jobs for free and people would still bitch.

Starver

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #773 on: September 26, 2012, 08:01:19 am »

While explaining the pointers well enough, I don't know if it answers the question of how pointers make (certain) things better.

It actually depends a bit on what you're trying to do, and some languages also need them more than others.  For a lot of things you don't need pointers, but for some things they're quite useful and for others they're damn useful.


And I've spent a while trying to write a full general explanation, but it all went rather TL;DR; and so I've now deleted all my confusing text in frustration.  But perhaps OP would like, in particular, to search himself for the terms "Pass By Reference" (when not the language default, anyway, this method of procedural call can be quite handy) and "Linked Lists" (and all kinds of kin, including bi-directionally linked ones, tree structures, webs of data, etc, where a simple array just won't do what you'd like).

There's a number of other applications, especially where you might want any particular two (or more!) variables to be synchronised (i.e., you want changes to one to be a change to (all the) the other(s)), but it's a complex or dynamic relationship that would be unwieldy to set up with static "SetAandBandCandF()"-type procedures/functions.  Instead, each of the variables are pointers (either initially 1:1 to static background set of plain values, or with dynamically created/destroyed targets as needed) and you just need to shift the B, C and F pointers to point at what A is pointing at (or any other combination) and now setting A or B or C or F-referenced values means that (until things are changed otherwise, at least) this value is immediately available when you peek at any of the other reference's end-values.

Darnit, started to go into "long explanation" mode, again.  I'll curtail it there.

OP: Is it specifically C-family pointers and uses that you're looking for?  They're pretty much a standard method of handling certain things, and interacting with the subsystem, but some of the things other languages use pointers for are already handled(/handleable) in other ways, perhaps more intuitive to the newer scholar of the art of programming.
Logged

Normandy

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #774 on: September 26, 2012, 04:03:50 pm »

It should be noted that all (non-const) variable names are pointers to memory holding the actual mutable data, regardless of what language you're programming in (well actually, that's not precisely true. Variable names are symbols which are associated with fixed spots in memory as well as other metadata). In almost all languages where doing Object foo = bar; simply sets foo to point to the same object as bar, what's actually going on there is a pointer assignment. Foo and bar are symbols referring to pointers to objects, not symbols referring to objects themselves (this is in contrast to 'primitive data types' where symbols actually refer to the objects in memory).

Exposing pointer arithmetic (which is what I presume you're actually wondering about) gives programmers clever ways to manipulate data they know the structure of. Pointer arithmetic is logically equivalent to array indexing in cases with uniform datatypes, which is why int *a = new int[5]; a[3]==3[a]; makes sense in C (since the [] operator in C is really just doing pointer arithmetic). However, it isn't exactly equivalent when dealing with classes and references. Generally speaking, when you do Object[] a; in most languages, what you're actually creating is an array of pointers to Objects, not an actual array of Objects; in C/C++, this is not the case. Object[] is actually a contiguous array of Objects in memory. There really isn't any way to tell the compiler in the former case to create a contiguous block of memory of Objects; in the latter case, without a specific pointer datatype, there is no way to differentiate between the two types of arrays.
Logged

DeadlyLintRoller

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #775 on: September 29, 2012, 08:47:35 pm »

Here I am, 9:45 on a Saturday night, working on class for a Master's in IT. Anyone have any Java Programming Questions? I am also experienced in PHP, Javascript and Drupal Development.
Logged
I'm back!

lorb

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #776 on: September 29, 2012, 09:26:33 pm »

There. Click that link if you really are that bored. Tons of java questions.
Logged
Please be gracious in judging my english. (I am not a native speaker/writer.)
"This tile is supported by that wall."

DeadlyLintRoller

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #777 on: September 29, 2012, 09:39:36 pm »

oh, gosh... I'll answer SO questions if I'm already researching an issue, but I'd rather not spend half my time trying to translate the question into an intelligible english phrase.
Logged
I'm back!

Starver

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #778 on: September 30, 2012, 08:29:32 am »

"What's the point of Java?"


(Naw, only kidding. ;) I love all God's programming languages.  Except Ada.  Can't get on with Ada.  No offence to Miss Lovelace herself, maysherestinpeace.)
Logged

Kofthefens

  • Bay Watcher
  • Keep calm and OH GOD CAPYBARAS
    • View Profile
    • Marshland Games
Re: Programming Help Thread (For Dummies)
« Reply #779 on: October 01, 2012, 09:43:25 am »

Here I am, 9:45 on a Saturday night, working on class for a Master's in IT. Anyone have any Java Programming Questions? I am also experienced in PHP, Javascript and Drupal Development.

If you need a question, this one never got adequately resolved.

I'm trying to make a program that will check a yahoo email. However, when I do emailChecker.recieve(), it gives me an error message. I am using JavaMail. The code looks like this:

Spoiler: Code (click to show/hide)

And the error message looks like this:
Spoiler (click to show/hide)

Thanks in advance for the help.
Logged
I don't care about your indigestion-- How are you is a greeting, not a question.

The epic of Îton Sákrith
The Chronicles of HammerBlaze
My website - Free games
Pages: 1 ... 50 51 [52] 53 54 ... 91