Algorithms For Dummies


Using Randomized Algorithms



Download 7,18 Mb.
Pdf ko'rish
bet533/651
Sana15.07.2021
Hajmi7,18 Mb.
#120357
1   ...   529   530   531   532   533   534   535   536   ...   651
Bog'liq
Algorithms

  Using Randomized Algorithms 

     327


to avoid picking a series of bad numbers. This is the strategy used by the Quickse-

lect and randomized Quicksort algorithms, described later in the chapter.

Because  algorithms  need  numeric  inputs,  knowing  their  distribution  can  help 

make them work smarter. It’s not just the initial distribution that counts. You can 

also take advantage of how data distribution changes as the algorithm proceeds. 

As an example of how a changing distribution can improve your algorithm, the 

following code shows how to guess a card in a French deck by random choice:

numbers = ['Ace','2','3','4','5','6','7','8','9','10',

                                'Jack','Queen','King']

seeds = ['Clubs','Spades','Diamonds','Hearts']

deck = [s

+'_'+n for n in numbers for s in seeds]

from random import choice

my_cards = deck.copy()

guessed = 0

for card in deck:

    if card == choice(my_cards):

        guessed 

+= 1

print ('Guessed %i card(s)' % guessed)



Guessed 1 card(s)

This strategy brings few results, and on average, you’ll guess a single card in all 

52 trials. In fact, for each trial, you have a 1/52 probability of guessing the correct 

FIGURE 17-2: 

A histogram  

of a uniform 

distribution.




328

 

   


  PART 5 


Download 7,18 Mb.

Do'stlaringiz bilan baham:
1   ...   529   530   531   532   533   534   535   536   ...   651




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2025
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish