Tag Info

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 ...
• 96.8k

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 ...
• 599

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: ...
• 11.5k
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 ...
• 17.7k
Accepted

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 ...
• 64.5k
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 ...
• 49.2k
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}$$ ...
• 142k

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 (...
• 652
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 ...
• 64.5k

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 ...
• 64.5k

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, ...
• 142k
Accepted

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

Generate random numbers without repetitions

Instead of using a List<int>, you should use an HashSet<int>. The HashSet<>...
• 12.3k
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: ...
• 4,215
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 ...
• 51k

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'...
• 309

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. ...
• 142k

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 ...
• 30.7k
Accepted

Generating a password to save to a .txt file

By making it more Pythonic for one: ...
• 306

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 ...
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 ...
• 5,513

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 ...
• 13.8k
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: ...
• 53.8k
Accepted

Completed Town Generator

There are two major concepts your code needs. Arrays Objects Arrays ...
• 58.2k

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 ...
• 40.6k
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: ...
• 40.6k

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 ...
• 1,354

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: ...
• 107k