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 ...
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 ...
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:
...
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 ...
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
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,
...
22
votes
Generate random numbers without repetitions
Instead of using a List<int>, you should use an HashSet<int>. The HashSet<>...
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 ...
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'...
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.
...
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 ...
19
votes
Accepted
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
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:
...
17
votes
Accepted
Completed Town Generator
There are two major concepts your code needs.
Arrays
Objects
Arrays
...
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 ...
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:
...
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 ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
random × 785python × 238
c++ × 119
beginner × 117
java × 116
performance × 86
python-3.x × 78
c# × 69
javascript × 64
strings × 55
simulation × 53
game × 48
dice × 39
algorithm × 38
c × 37
php × 36
array × 36
c++11 × 31
object-oriented × 24
python-2.x × 23
shuffle × 21
security × 19
numerical-methods × 18
haskell × 17
statistics × 17