Lambobin bazuwar mabuɗin ra'ayi ne a cikin shirye-shirye waɗanda ke nemo mai amfani a cikin kewayon aikace-aikace. Suna taka muhimmiyar rawa a fannoni daban-daban kamar cryptography, simulations, gwaji, da wasanni. Musamman a cikin C #, samar da adadin bazuwar tsakanin 1 zuwa 100 yana da ɗimbin abubuwan amfani. A cikin wannan labarin, za mu bincika yadda ake cim ma wannan aikin a cikin C #, ƙara nutsewa cikin ɗakunan karatu, ayyuka, da bayanin mataki-mataki na lambar.
C # Random Class
The C# Random class, ginanniyar ajin da ake samu a cikin Tsarin sunaye, yana ba da ayyuka don samar da lambobi bazuwar. A cikin ajin Random, akwai hanyoyi da yawa, amma biyun da aka fara amfani da su sune Next() da NextBytes(). Don samar da adadin bazuwar a cikin kewayon 1 zuwa 100, muna amfani da hanya ta gaba().
Random bazuwar = sabon bazuwar ();
int randomNumber = bazuwar.Na gaba (1, 101);
Kamar yadda kuke gani a cikin misali, an ƙaddamar da "Random" kuma daga baya aka yi amfani da shi don samar da lambar bazuwar. Ana kiran hanyar ta gaba() tare da sigogi biyu: mafi ƙaranci da babba (keɓaɓɓe) iyaka. Wannan nau'in siga guda biyu na aikin na gaba zai samar da lambar bazuwar da ta fi girma ko daidai da siga na farko, kuma ƙasa da siga na biyu..
Fassarar Mataki-mataki na Code
- Na farko, an ƙirƙiri misali na ajin bazuwar tare da layin 'Random bazuwar = sabon Random();'. Za a yi amfani da wannan abu don samar da lambar bazuwar mu.
- Na biyu, muna kiran aikin na gaba () akan abu na Random tare da sigogi na 1 da 101. Kamar yadda waɗannan keɓaɓɓun keɓaɓɓu ne da keɓantattun iyakoki, lambar da aka ƙirƙira zata kasance cikin kewayon 1-100.
Aiwatar da layin lambar "bazuwar.Na gaba(1, 101)" zai samar da adadin bazuwar tsakanin 1 da 100.
Nutsuwa Mai Hankali cikin Tsarin Sunan Tsarin
A cikin C#, Tsarin sunan tsarin ya ƙunshi azuzuwan asali da azuzuwan tushe waɗanda ke ayyana ƙimar da aka saba amfani da su da nau'ikan bayanai, abubuwan da suka faru da masu gudanar da taron, musaya, halaye, da kebantattun sarrafawa.
Wurin suna System shine uwar uwar azuzuwan .NET da yawa. Misali, ajin Random, wanda muka tattauna, yana cikinsa daidai. Yawancin sauran azuzuwan kamar DateTime, Math, String, da ƙari suma suna cikin ɓangaren tsarin sunaye, kasancewa tushen tushe a yawancin shirye-shiryen C #.
A ƙarshe, yayin da muka zo cikakke a cikin fahimtarmu na samar da lambobi bazuwar a cikin C #, a bayyane yake cewa tare da ingantaccen ilimin azuzuwan da hanyoyin haɗin gwiwa, aikin yana da sauƙi. Yana da mahimmanci a tuna cewa C # da ɗakunan karatu suna ba da kayan aiki masu ƙarfi kamar ajin Random don taimakawa masu shirye-shirye don cimma ayyukan da ake so da kyau.