54
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 ...
41
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 ...
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 ...
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 ...
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}$$
...
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 (...
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 ...
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 ...
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, ...
23
votes
Laughably tiny random engine
Your question has two parts:
Is the algorithm good?
Is the implementation good?
The first part is not really the purview of code review, because it is not only a mathematical question, it is an ...
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:
...
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 ...
21
votes
Accepted
Python \ random password generation code
Don't use random for security
Quoting from the documentation for random:
Warning: The pseudo-random generators of this module ...
20
votes
Python program to scramble MP3 audio
Consider what happens if a user closes the "Load MP3" dialog without picking a file. In that case, filepath would get set to ...
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 ...
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 ...
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 ...
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 ...
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:
...
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 ...
17
votes
Accepted
Python command-line program that generates passwords
Put all code in functions. Code in functions can be tested, experimented
with, and reorganized much more easily than code floating around at the top
level. Floating code introduces the temptation to ...
16
votes
Accepted
Random string of variable length generator
Just two remarks:
RandomChar should be a method because it returns a different result each time - this is just a convention that we usually follow in C#
In ...
16
votes
Python program to scramble MP3 audio
There is quite some room for improvement, both in the overall architecture and style and in the logic that you use:
Architecture and style
Separate logic from presentation
You have a single class ...
16
votes
Accepted
Laughably tiny random engine
Possibility of short cycles
SHA256 is a cryptographically secure hash function, which indeed means the output is very uncorrelated to the input. However, given exactly the same input, it will always ...
15
votes
Accepted
Pick a random big word from a list of words using Python
Comments
Python functions are written in snake_case.
You could use function parameters for word length and filename.
You could use iterators and generators to ...
15
votes
20 Ways to do Random Sampling
Besides the good points of @Graipher's answer:
Examples n° 6, 7, 10, 11, 12, 14, 15, 16, and 20 are the only one that modify the input data and thus only them need to copy the input to not modify it.
...
15
votes
Markov chains to generate text
Functions
Split the code into functions, also split the generation and the presentation. Your algorithm has some clear distinct tasks, so split along these lines:
read input
assemble chain
construct ...
15
votes
Accepted
Generating a unique key
You can pass str to map rather than a lambda.
You can remove the need for ...
14
votes
Seed std::mt19937 from std::random_device
I wrote more or less exactly the same function for my own use so of course I think it is pretty awesome. ;-)
Two things that I would do differently (only style, not security):
Don't hard-code the ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
random × 800python × 250
beginner × 120
c++ × 118
java × 117
performance × 87
python-3.x × 80
c# × 70
javascript × 65
strings × 55
simulation × 52
game × 51
dice × 40
c × 39
algorithm × 37
php × 37
array × 36
c++11 × 29
object-oriented × 24
python-2.x × 23
shuffle × 21
security × 19
numerical-methods × 18
haskell × 17
statistics × 17