Hot answers tagged

105 votes
Accepted

Generate random numbers without repetitions

Updated answer in response to bounty: See Is that your final answer? at the end, and other changes - basically answer is significantly rewritten. To break your problem down in to requirements: you ...
user avatar
  • 96.8k
53 votes

Random IP Address Generator

Python is often described as a "batteries included" kind of language, and this is no exception. There's a module just for IP address manipulation and another module to generate random numbers. Put ...
user avatar
  • 599
40 votes

Generate random numbers without repetitions

Yes, there definitely is. You generate a collection of elements, mash it around and start pulling items out of it. A quick oneliner would be: ...
user avatar
39 votes
Accepted

Seed std::mt19937 from std::random_device

Well, first off, why do you use a std::vector for a comparatively small sequence of known length? A raw array or std::array ...
user avatar
  • 17.7k
38 votes
Accepted

Random password generator in C

I see some things that I think could help you improve your code. Decompose your program into functions All of the logic here is in main in one rather long and ...
user avatar
  • 64.5k
36 votes
Accepted

20 Ways to do Random Sampling

It would be worth your while to take a look at the implementation of random.sample to see how it works. (It's different from all 20 implementations in the post, and ...
user avatar
  • 49.2k
30 votes
Accepted

6 out of 49 Lottery

Bug Winning a 6/49 game is, of course, unlikely. The probability of any single ticket having all six numbers correct is $$\dfrac{1}{\binom{49}{6}} = \dfrac{6!\,(49-6)!}{49!} = \dfrac{1}{13983816}$$ ...
user avatar
29 votes

Generate a list of 10,000 numbers in random order

In general First and foremost, you were asked to produce a program that generates a list of 10,000 numbers in random orders. You've added far too much complexity. There's no need for input or output (...
user avatar
  • 652
28 votes
Accepted

C++ Coin flip simulator and data collector

There's a lot that can be improved here, so I hope that these suggestions are useful to you. Don't abuse using namespace std Putting ...
user avatar
  • 64.5k
26 votes

Random IP Address Generator

Here are a few ideas about your code. Check for command line arguments The code fails with an exception if it's invoked with no command line arguments because it attempts to use ...
user avatar
  • 64.5k
24 votes

Random alphanumeric password generator with GOTOs

Data structure Why are you using a LinkedList<char>? Linked lists involve a lot of overhead for each node. You know exactly how long the result should be, ...
user avatar
23 votes
Accepted

Generating random email addresses

From this Stack Overflow question : import random foo = ['a', 'b', 'c', 'd', 'e'] print(random.choice(foo)) In your case, ...
user avatar
  • 28.7k
22 votes

Generate random numbers without repetitions

Instead of using a List<int>, you should use an HashSet<int>. The HashSet<>...
user avatar
  • 12.3k
22 votes
Accepted

Randomly generate spelling mistake in string

Break it down into one function that randomly repeats a character, and another function which randomly executes that function 50% of the time: ...
user avatar
  • 4,215
22 votes
Accepted

Simple Python code that makes a random string of letters and numbers

Don't use caps, numbers and letters; those are all constants available from the ...
user avatar
20 votes

Generate random numbers without repetitions

Expanding on my comment. This is called a linear congruential generator. I used common parameters, which come from what I think is called the Minimal Standard. Other parameters can be chosen, but that'...
user avatar
  • 309
19 votes

Generating 1000 random numbers

Speed and efficiency Just don't worry about it. You need to generate 1000 random numbers, and do a little bit of accounting work. Any reasonable solution, such as yours, will perform similarly. ...
user avatar
19 votes

License and Registration, Sir

Consider the real world scenario a little bit. Does a car know what the speed limit is? Does it issue itself a fine if it's speeding? Or do the police do this? Assuming we're talking about the ...
user avatar
  • 30.7k
19 votes
Accepted

Generating a password to save to a .txt file

By making it more Pythonic for one: ...
user avatar
  • 306
19 votes

Random IP Address Generator

For someone not familiar with Python, you have picked pretty good habits. Not everyone uses functions or the if __name__ == '__main__' guard first try. That being ...
user avatar
18 votes
Accepted

Solving for Bananas

General Feedback The good thing is that it is quite clear what your code does. You have for the most part clear variable names and not too much clutter. However I would change ...
user avatar
18 votes

Create two random teams from a list of players

Review Don't use the main entry point to implement an algorithm. Create a method instead. Think about how to allow this method to be usable for all kinds of types, not just integers. You have ...
user avatar
  • 13.8k
17 votes
Accepted

Randomized response generator for number-guessing game

A standard technique is to setup an array of strings and index it with a random number: ...
user avatar
  • 53.8k
17 votes
Accepted

Completed Town Generator

There are two major concepts your code needs. Arrays Objects Arrays ...
user avatar
17 votes

20 Ways to do Random Sampling

You should not use eval without a very good reason. This is not a very good reason. Even though here you are actually fully in control of what gets passed to ...
user avatar
  • 40.6k
17 votes
Accepted

Generate 10 random 3-letter strings

Python 3.6 introduced random.choices, which allows you to write this even more succinctly than using random.choice: ...
user avatar
  • 40.6k
17 votes

A dice game called "Greed"

Style You're not being charged by the character; there's no need to abbreviate "count", or "ret" (which I would call "score" instead). Also, main has inconsistent brackets with the rest of the ...
user avatar
  • 1,354
16 votes

Simple random number generator

Code organization Rather than having your code in the global namespace, it's better to wrap it into a method, for example: ...
user avatar
  • 107k
16 votes
Accepted

Proof that I was wrong about Random Number Generators

You've implemented a program to produce a histogram (though in tabular rather than visual form). That lets you judge "by eyeball" whether the distribution looks uniform. You would expect each bin to ...
user avatar

Only top scored, non community-wiki answers of a minimum length are eligible