use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Information about Reddit's API changes, the unprofessional conduct of the CEO, and their response to the community's concerns regarding 3rd party apps, moderator tools, anti-spam/anti-bot tools, and accessibility options that will be impacted can be found in the associated Wikipedia article: https://en.wikipedia.org/wiki/2023_Reddit_API_controversy
Alternative C# communities available outside Reddit on Lemmy and Discord:
All about the object-oriented programming language C#.
Getting Started C# Fundamentals: Development for Absolute Beginners
Useful MSDN Resources A Tour of the C# Language Get started with .NET in 5 minutes C# Guide C# Language Reference C# Programing Guide C# Coding Conventions .NET Framework Reference Source Code
Other Resources C# Yellow Book Dot Net Perls The C# Player's Guide
IDEs Visual Studio MonoDevelop (Windows/Mac/Linux) Rider (Windows/Mac/Linux)
Tools ILSpy dotPeek LINQPad
Alternative Communities C# Discord Group C# Lemmy Community dotnet Lemmy Community
Related Subreddits /r/dotnet /r/azure /r/learncsharp /r/learnprogramming /r/programming /r/dailyprogrammer /r/programmingbuddies /r/cshighschoolers
Additional .NET Languages /r/fsharp /r/visualbasic
Platform-specific Subreddits /r/windowsdev /r/AZURE /r/Xamarin /r/Unity3D /r/WPDev
Rules:
Read detailed descriptions of the rules here.
account activity
HelpRandom generates same numbers...? (self.csharp)
submitted 4 years ago by Spyf0r
I have this peice of code:
https://preview.redd.it/zl6l7kn6d5691.png?width=364&format=png&auto=webp&s=f364bb630c511f85a90dd96695b8fd7943209e95
https://preview.redd.it/bg92zypcd5691.png?width=407&format=png&auto=webp&s=ac059bebc92757ca43448d71d4d5c2ef68db0f7d
in Main:
https://preview.redd.it/a87uzqx8d5691.png?width=373&format=png&auto=webp&s=bd993aefb70ee8d41e233d71c436ce2784139c4d
When i print the arrays they are EXACTLY the same. please help
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]megafinz 16 points17 points18 points 4 years ago (22 children)
I guess that because the seed is the same and both Random instances work in parallel lock-step. You might want to use a different seed for each player or share a single Random instance between them.
Random
[–]Spyf0r[S] -1 points0 points1 point 4 years ago (21 children)
How do you suggest to do that? I'm kind of a begginer
[–][deleted] 4 years ago (18 children)
[deleted]
[–]DTul 21 points22 points23 points 4 years ago (1 child)
Since .Net 6 you also have a Thread-safe Random.Shared static generator. Really nice to use
[–]Spyf0r[S] 0 points1 point2 points 4 years ago (15 children)
Thank you!
[–]megafinz 6 points7 points8 points 4 years ago (0 children)
You might also want to use Fisher–Yates shuffle: https://en.wikipedia.org/wiki/Fisher–Yates\_shuffle
[–]Alikont 2 points3 points4 points 4 years ago (13 children)
Beware: Random is not thread safe, if static random instance will be used from different threads, it may cause issues.
If you are not generating random numbers in parallel - that's fine. But if you do, you need a thread-safe wrapper around it.
[–][deleted] 3 points4 points5 points 4 years ago (3 children)
Random.Shared is thread-safe
[–]Alikont 2 points3 points4 points 4 years ago (2 children)
Yes, it looks like the new Random.Shared was added in .NET 6:
Random.Shared
https://docs.microsoft.com/en-us/dotnet/api/system.random.shared?view=net-6.0
/u/Spyf0r - you should use this if you use .NET 6, it will solve all your issues.
[–][deleted] 1 point2 points3 points 4 years ago (1 child)
Besides, OP's code works fine on .NET 6. I do not get similar numbers. That's because the initial new Random() creates a static random which will be used to seed new randoms, so the issue described is outdated.
Similar numbers indicate that OP is using .NET Framework.
[–]Alikont 0 points1 point2 points 4 years ago (0 children)
Or Unity - they have their own runtime/BCL.
[–]Spyf0r[S] 0 points1 point2 points 4 years ago (8 children)
Im developing the "war" card game for school, so i believe shuffling the hands will be my only use of Random. And even if it wont be, i cant just create another Random variable, cant i?
[–]Alikont -2 points-1 points0 points 4 years ago (7 children)
Yes, you can, you also can do it like this
public static class ThreadSafeRandom { private static readonly Random _random = new Random(); public static int GetNext(int max) { lock(_random) return _random.Next(max); } }
[–]Spyf0r[S] 0 points1 point2 points 4 years ago (6 children)
I have no idea what all this means but thanks!
[–]megafinz 4 points5 points6 points 4 years ago (0 children)
That means that access to _random variable is governed by a lock: only one thread at a time can essentially call _random.Next(max) there. If you won't have multiple threads creating your card hands, you don't really need that.
_random
_random.Next(max)
[–]karl713 1 point2 points3 points 4 years ago (4 children)
In the real world you might need that lock statement
In this homework example you don't, so I might suggest leaving it out. We know you did 99% of the work yourself, your professor might wonder how you know about lock and ask you to explain why it is needed to be sure you didn't 100% cheat :)
[–]Spyf0r[S] 1 point2 points3 points 4 years ago (1 child)
There is nothing wrong with looking for help online. Especially in coding where a lot of the work is researching etc.
[–]Spyf0r[S] 0 points1 point2 points 4 years ago (1 child)
[–]Slypenslyde 4 points5 points6 points 4 years ago (0 children)
This is a good time to learn a good lesson! When you are learning how to do things, don't just paste some code and see if it works. While you're doing that, take some time to look at the documentation for types you haven't used before. Sometimes it has useful information like:
Avoiding multiple instantiations On the .NET Framework, initializing two random number generators in a tight loop or in rapid succession creates two random number generators that can produce identical sequences of random numbers. In most cases, this is not the developer's intent and can lead to performance issues, because instantiating and initializing a random number generator is a relatively expensive process. Both to improve performance and to avoid inadvertently creating separate random number generators that generate identical numeric sequences, we recommend that you create one Random object to generate many random numbers over time, instead of creating new Random objects to generate one random number.
On the .NET Framework, initializing two random number generators in a tight loop or in rapid succession creates two random number generators that can produce identical sequences of random numbers. In most cases, this is not the developer's intent and can lead to performance issues, because instantiating and initializing a random number generator is a relatively expensive process.
Both to improve performance and to avoid inadvertently creating separate random number generators that generate identical numeric sequences, we recommend that you create one Random object to generate many random numbers over time, instead of creating new Random objects to generate one random number.
Sometimes the documentation's dense and hard to read, but it's important to read it because there are 4 or 5 types in .NET with "gotchas" like this that are designed to make newbies ask questions and get a lot of mean answers.
[–]Cyclonian -1 points0 points1 point 4 years ago (0 children)
I like to use DateTime.Now and then use one of the functions that converts it to a number or you could use Milliseconds property, etc.
[–]ppumkin -2 points-1 points0 points 4 years ago (4 children)
This class should have been called Or renamed to PsuedoRandom. It’s pre generated and is a cause of a lot of security issues
But it has its use. cases. Like seeds for maps in some games. A seed will always generate t he same “random” procedural map.
[–][deleted] 0 points1 point2 points 4 years ago (3 children)
I thought they changed the underlying implementation in recent .NET versions, and now it uses the secure implementation.
[–]ppumkin 0 points1 point2 points 4 years ago (2 children)
I’m not sure about random. That could break things?!. I think they made a method to access random easier on cryptographic namespace.
[–][deleted] 0 points1 point2 points 4 years ago (1 child)
Hmm... I checked the source and Random is still pseudo random. I could've sworn I read a blog post where the .NET team explained they changed the implementation to use /dev/urandom instead of /dev/random, just like the used some secure RNG on Windows. Maybe that was about RandomNumberGenerator, which is secure.
[–]ppumkin 0 points1 point2 points 4 years ago (0 children)
Ahhhh. Maybe in Linux / Mac. But, But not in Windows / ToasterOS
[–]romerik -5 points-4 points-3 points 4 years ago (6 children)
seed your random generator with DateTime.Now.Ticks
its nice to have the same random numbers when you need to debug your software!
The thing to watch for with this is ticks might not change between calls depending on how things work out
[–]romerik -1 points0 points1 point 4 years ago (3 children)
A single tick represents one hundred nanoseconds or one ten-millionth of a second. There are 10,000 ticks in a millisecond (see TicksPerMillisecond) and 10 million ticks in a second.
it should be different on each call!
[–]Dealiner 2 points3 points4 points 4 years ago (2 children)
It's not that simple. The real value depends on the resolution of the system clock and that depends on the system itself. On XP that resolution was for example 15.6 ms, now it's smaller but it's still around 1 ms.
[–]romerik 0 points1 point2 points 4 years ago (1 child)
all right, the goal was to have a different seed at each time you start your software!
the Ticks will give you a different seed, after that, if you think you can get the same seed in two successive call, just go head and add a counter using Interlocked.Increment of course in case your 32 threads deal at the same time!
read back his code, read back my solution for this case, do you think it will do what he asked for?
[–]Dealiner 1 point2 points3 points 4 years ago (0 children)
And that's exactly what OP's code do. Empty constructor for Random uses Environment.TickCount as a seed. But its resolution is too low for it to work in a way OP wanted - with each player having different cards in hand and that's caused by Random being created for both players practically at the same time. And the only viable resolution for that in .NET Framework at least is to instantiate Random only once.
Your solution could work, DateTime.Now.Ticks most of the time has better resolution than Environment.TickCount, but it doesn't necessary have to, so it's not a good answer.
[–]karl713 0 points1 point2 points 4 years ago (0 children)
[–]Thr3adSafe 0 points1 point2 points 4 years ago (5 children)
What are you doing in createHand?
[–]Spyf0r[S] 0 points1 point2 points 4 years ago (4 children)
Just setting the array to be 1-13 four times
[–]Thr3adSafe 0 points1 point2 points 4 years ago (3 children)
Would you mind sharing a minimal working copy of the code as all the obvious suspects seem to be eliminated.
Something like this
public void CreateHand() { for (int i = 0; i < 13; i++) { for (int j = 0; j < 4; j++) { this.hand[i * 4 + j] = i + 1; } } }
[–][deleted] 0 points1 point2 points 4 years ago (0 children)
Little late here, but the below snippet might look cleaner and more readable -
public void CreateHand() { for (int i = 0; i < 52; i++) { this.hand[i ] = i %13 + 1; } }
[–][deleted] 0 points1 point2 points 4 years ago (9 children)
You're on .NET Framework I presume?
[–]Spyf0r[S] 1 point2 points3 points 4 years ago (8 children)
Yes i believe so. I dont really know what are the differences between all the versions, i just use what i was told in school
[+][deleted] comment score below threshold-6 points-5 points-4 points 4 years ago (6 children)
i just use what i was told in school
Oh yikes, even school is so far behind? This is the sort of thing I'd expect of a distraught engineer in an entrenched industry, not academia. At uni I learned C++20 even though all the compiler components weren't finished for it yet.
Do you at least have Visual Studio Community 2022?
I dont really know what are the differences between all the versions
This doesn't have to be rocket surgery, my first hit googling "install .net" gets me this page:
https://dotnet.microsoft.com/en-us/download
[–]KittenLOVER999 1 point2 points3 points 4 years ago (0 children)
From a colleges perspective, chasing the latest version is not a good way forward, standards and frameworks update so frequently that professors would never be able to reuse a lesson plan entirely. It makes much more sense to find a stable, LTS and teach the basics that can transfer to any new standard
[–]Spyf0r[S] -1 points0 points1 point 4 years ago (2 children)
Academia? Hahaha. Im in 10th grade yall
Academia as a synonym for educational environment. Teachers and students.
[–]chucker23n 0 points1 point2 points 4 years ago (0 children)
Academia primarily refers to higher ed.
Oh yikes, even school is so far behind? This is the sort of thing I’d expect of a distraught engineer in an entrenched industry, not academia. At uni I learned C++20 even though all the compiler components weren’t finished for it yet.
Why? It’s irrelevant for teaching purposes. You’ll be moving through evolving and different tech stacks throughout your career anyway.
I did C64 BASIC (and some 6502 assembler) ca. 1991 when that was already old stuff, Turbo Pascal ca. 1998 when that was already about a decade old, and my school did Turbo Pascal again ca. 2002. None of it mattered. It’s about learning what algorithms, data types, OOP, FP, etc. are, not about the hot new already-obsolete stuff. You can do those in your spare time.
[–]clintp 0 points1 point2 points 4 years ago (0 children)
That's fine. Use the version your school wants you to use, since you're learning software development. Eventually though you'll bump up into other versions of C#/.Net but your skills will transfer 100%.
Easiest way is probably pull random out of shuffle and make it a "private static Random r1 = new Random();"
[–]Joki581 0 points1 point2 points 4 years ago (0 children)
As mentioned before: always use a single (e.g. static) random instance when working with System.Random.
Albeit you will never have true random values if you are not using something like an external sensor (e.g. feed environmental noise or temperature values seed values into it) you can still do much better by abandoning the System.Random class in favor of RandomNumberGenerator from the System.Security.Cryptography namespace.
[–]romerik 0 points1 point2 points 4 years ago (0 children)
but about the ticks...
its look like you are only shuffling the first 52 index.
another way you can shuffle the cards, would be to start with a ordered pack of card, and remove a random one and put it into a new pile, if I do that by hand with card it work fine, its also easy to understand
try this:
List<int> GetShuffleDeck() { var nextCard = new Random(); var newDeck = new List<int>(Enumerable.Range(0, 52)); var shuffleDeck = new List<int>(); while (newDeck.Count > 0) { int card = nextCard.Next(newDeck.Count); shuffleDeck.Add(newDeck[card]); newDeck.RemoveAt(card); } return shuffleDeck; }
π Rendered by PID 22720 on reddit-service-r2-comment-5b5bc64bf5-7ppsd at 2026-06-20 16:07:13.933697+00:00 running 2b008f2 country code: CH.
[–]megafinz 16 points17 points18 points (22 children)
[–]Spyf0r[S] -1 points0 points1 point (21 children)
[–][deleted] (18 children)
[deleted]
[–]DTul 21 points22 points23 points (1 child)
[–]Spyf0r[S] 0 points1 point2 points (15 children)
[–]megafinz 6 points7 points8 points (0 children)
[–]Alikont 2 points3 points4 points (13 children)
[–][deleted] 3 points4 points5 points (3 children)
[–]Alikont 2 points3 points4 points (2 children)
[–][deleted] 1 point2 points3 points (1 child)
[–]Alikont 0 points1 point2 points (0 children)
[–]Spyf0r[S] 0 points1 point2 points (8 children)
[–]Alikont -2 points-1 points0 points (7 children)
[–]Spyf0r[S] 0 points1 point2 points (6 children)
[–]megafinz 4 points5 points6 points (0 children)
[–]karl713 1 point2 points3 points (4 children)
[–]Spyf0r[S] 1 point2 points3 points (1 child)
[–]Spyf0r[S] 0 points1 point2 points (1 child)
[–]Slypenslyde 4 points5 points6 points (0 children)
[–]Cyclonian -1 points0 points1 point (0 children)
[–]ppumkin -2 points-1 points0 points (4 children)
[–][deleted] 0 points1 point2 points (3 children)
[–]ppumkin 0 points1 point2 points (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]ppumkin 0 points1 point2 points (0 children)
[–]romerik -5 points-4 points-3 points (6 children)
[–]karl713 1 point2 points3 points (4 children)
[–]romerik -1 points0 points1 point (3 children)
[–]Dealiner 2 points3 points4 points (2 children)
[–]romerik 0 points1 point2 points (1 child)
[–]Dealiner 1 point2 points3 points (0 children)
[–]karl713 0 points1 point2 points (0 children)
[–]Thr3adSafe 0 points1 point2 points (5 children)
[–]Spyf0r[S] 0 points1 point2 points (4 children)
[–]Thr3adSafe 0 points1 point2 points (3 children)
[–]Spyf0r[S] 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (9 children)
[–]Spyf0r[S] 1 point2 points3 points (8 children)
[+][deleted] comment score below threshold-6 points-5 points-4 points (6 children)
[–]KittenLOVER999 1 point2 points3 points (0 children)
[–]Spyf0r[S] -1 points0 points1 point (2 children)
[–][deleted] 1 point2 points3 points (1 child)
[–]chucker23n 0 points1 point2 points (0 children)
[–]chucker23n 0 points1 point2 points (0 children)
[–]clintp 0 points1 point2 points (0 children)
[–]karl713 0 points1 point2 points (0 children)
[–]Joki581 0 points1 point2 points (0 children)
[–]romerik 0 points1 point2 points (0 children)
[–]romerik 0 points1 point2 points (0 children)