Hello Z,
in plain C that would be
A = (rand() % 100) + 1;
Yes, that would get a random number between 1 and 100 inclusive. What it won't do is give equal probability to each.
Hello Z,
in plain C that would be
A = (rand() % 100) + 1;
i think what he was saying was along the lines of:Hello dasmiller,
may i ask you what compiler you used? Because i never ever had that problem. What matters in the declaration is that the type(s) of the argument(s) match the implementation. The names are completely irrelevant usually. I know that _some_ compilers can generate, if instructed to do so, empty implementations of a function if there is no implementation given in the source, but only the declaration.s
int secondsfromminutes(int minutes, int seconds);
int secondsfromminutes(int seconds, int minutes)
{
return (minutes*60) + seconds ;
}
Hello Z,
in plain C that would be
A = (rand() % 100) + 1;
to get a value between 1 and 100 (both inclusive). I think your example has a bug, it would give you numbers from 1 to 101 (101 in the case that RND(1) returns 1).
However, in C you also need to seed the random number generator using something like:
srand(time(NULL));
Instead of time(NULL) you can use a fixed value instead, if you need reproducible streams of random numbers upon each startup.
Edit: in case you wonder, % is the modulo operator. It gives you the remainder of a division. So, 136 % 100 = 36, 593 % 100 = 93, etc...
OTOH, it's easy to make applications in just a few minutes. One thing I can't figure out - what are the usual methods for randomization? I remember in BASIC we had a way to do it that looked something like:
A = INT(RND(1)*100)+1
that would produce a random integer from 1 to 100 - how on earth do we do that now?
Well... back to lurk for me.
private int RandomNumber(int min, int max) {
Random random = new Random();
return random.Next(min, max);
}
I think Roger nails it. I programmed C++ for many years and used both Stroustrup's book and annotated reference extensively. Then I took a close look at Java and C#, and that's when I realized what a fantastic job Stroustrup did designing the language. Some of the things in C++ look weird or obscure when you first encounter them, but they are the product of years and years of careful thought, and their virtues are glaringly obvious when you look at another language.Well, I mostly adore C++, for the reasons others criticize it. Bjarne's book on how and why he designed C++ the way he did is very instructive for those who disparage the language...
My experience was that bad C++ code was the product of bad programming, not bad language design. C++ is admittedly a very difficult language to master, but well worth the effort. One of the most powerful features of the language is the STL (standard template library), and if you use and extend its patterns you will never see a pointer or even see 'new' and 'delete'. With correct factorization of responsibilities it is possible to write very hierarchical code in which complexity at any given level is close to zero.C++ is nearly self-encrypting and encourages a lot of (IMO) bad practices. ETA: Gonna guess that Roger (post #5) has a different perspective on that.
I couldn't disagree more. As pointed out by Roger, the ability to span from low-level to very abstract is C++'s great strength. For example, C# is wonderful if you are creating a GUI. But try to manage data efficiently and you are screwed. .NET did introduce templates in version 3 (a belated recognition of a serious omission), but by comparison to the STL its containers suck.That of the four languages you mention, I only like C. C++ (and likewise Objective-C) has two orthogonal feature sets layered on top of one another, procedural and OO, which haven't been integrated together very well. C++ has the misfortune of neither being the simple, clear system-level programming language that C is, or possessing any of the advanced abstractions high level languages provide.
i think what he was saying was along the lines of:
Code:int secondsfromminutes(int minutes, int seconds); int secondsfromminutes(int seconds, int minutes) { return (minutes*60) + seconds ; }
The declaration and implementation assume a different order of variables.
int secondsfromminutes(int , int);
int secondsfromminutes(int seconds, int minutes)
{
return (minutes*60) + seconds ;
}
In C#..
private int RandomNumber(int min, int max) {
Random random = new Random();
return random.Next(min, max);
}
With that function declared, you would get a random number in a desired range via:
aParticularRandomNumber = RandomNumber(1, 100)
My experience was that bad C++ code was the product of bad programming, not bad language design.
You're not really wrong. There are performance issues, not necessarily to the extent you're describing, but it can still be as much as 20%.Okay, I'm probably wrong on that. I wrote a OpenGL program in straight C++, and then my boss wanted to see if we could switch to a commercial product that used Java, all kinds of third party libraries, all on top of OpenGL. As you might imagine, they had huge performance issues - I'd pop up and start running in a second or two, they'd take a minute to two minutes. And they were hamstrung, having no way to tune all the compenents they were using. But that is more an issue of using third party libraries, not the language. I stand corrected.
Well, the auto-completion features of Visual Studio are excellent (and context-sensitive), so you don't actually have to type that much.The technology has advanced, and computers can do more than ever before... but to get a random number, we have to type all that rather than the old way... Shouldn't it get easier, rather than harder?
If you want to do that kind of stuff though, you still have the option of separating that component out into native code and simply calling it from your .NET/Java application. That's done all the time.When you're doing bit-fiddling with binary files, it's got to be C++. Using a language that hides pointers in that situation is like threading a needle wearing boxing gloves.
Hello dasmiller,
may i ask you what compiler you used? Because i never ever had that problem. What matters in the declaration is that the type(s) of the argument(s) match the implementation. The names are completely irrelevant usually. I know that _some_ compilers can generate, if instructed to do so, empty implementations of a function if there is no implementation given in the source, but only the declaration.
If you really had that bug because of only the names, i would tend to say that this is a severe bug in the compiler instead.
On a sidenote, you don't need to give any variable names in the declaration at all. Something like "int myfunc(int, int);" should be enough.
...
I happen to use C# the most (when I am not stuck with VB). But, perhaps that is mostly due to historic accident. I have always been a Microsoft-platform developer, in my professional career, and I suppose there is little chance of that changing, any time soon.
What do you think?
When you're doing bit-fiddling with binary files, it's got to be C++. Using a language that hides pointers in that situation is like threading a needle wearing boxing gloves.
Data Length Number of Codes Codes
0 1 000
1 2 0010 and 0011
2 4 01000, 01001, 01010 and 01011
.
.
This is how you would print 100 random numbers in C++In C#
Code:private int RandomNumber(int min, int max) { Random random = new Random(); return random.Next(min, max); }
generate_n(ostream_iterator<double>(cout, "\n"), 100, rand);
That changes nothing. If the caller thinks the order is minutes/seconds, and the implementer thinks the order is seconds/minutes, you still have the same problem.And what he is saying is that there's no reason to code it as above. Instead you should code:
Code:int secondsfromminutes(int , int); int secondsfromminutes(int seconds, int minutes) { return (minutes*60) + seconds ; }
// ~10 lines of instructions for class usage goes here
class HMS
{
private:
HMS ();
public:
// assignment functions
friend HMS AssignHMS (int hours, int minutes, int seconds);
friend HMS Seconds (int seconds);
... and a few more
// arithmetic operators
HMS operator+ (const HMS& time);
... etc
};
HANDLE CreateButton (int color, int width, int height);
HANDLE h = CreateButton (color => red, width=>50, height=>20);
In C#
With that function declared, you would get a random number in a desired range via:Code:private int RandomNumber(int min, int max) { Random random = new Random(); return random.Next(min, max); }
aParticularRandomNumber = RandomNumber(1, 100)
private int RandomNumber(int min, int max) {
Random random = new Random();
return random.Next(min, ++max);
}
Today, I am inclined to think that ASP.NET 1.1 was rather ghastly. Have you seen the newer versions? They are substantially less ghastly, since the advent of partial classes, Master pages, generics, improved @page properties, and other things.
Example: why doesn't C++ have garbage collection? For a couple of very good reasons. Firstly, you don't need it! If you adhere to the creation-is-resource-acquisition and destruction-is-resource-release paradigm, and you use stack-based constructors for resource management, you just can't go wrong. Secondly, with garbage collection you can't control exactly when a resource gets released. Thirdly, garbage collection is just very, very inefficient. Fourthly, garbage collection is complicated by mutual references. And so on. Java and C# designers just didn't think through all the implications, and consequently it is difficult to write inherently efficient and well managed code in either of those languages.
My experience was that bad C++ code was the product of bad programming, not bad language design.
But at the end of the day, its the quality of the programmer that's most important.
In BASIC - at least the versions I used - the INT(RND) function returned a number from 0.00 to 0.99. Multiplying by 100 gives 0 to 99; hence, you had to add the +1 at the end.
Why did we have to start seeding random number generators, anyway?
Why did we have to start seeding random number generators, anyway?