[Tutor] question

Alan Gauld alan.gauld at yahoo.co.uk
Wed Apr 12 05:27:31 EDT 2023


On 12/04/2023 07:28, Prayash Bhuyan wrote:
> I know how to generate random numbers using random function but I want to
> know how do i generate random numbers say between (1,5) where each random
> number can only be repeated only 4 times........please help

You have to write code to do it.

If you limit the number of times a number can be used it becomes
increasingly less random. (In your case after you generate 19 numbers
you know what the 20th will be.) The random functions generate an
continuous stream of numbers all of which are random. (Even if
theoretically that results in them all being the same!)

So you will need to create a function that checks whether the number
has already appeared and if so how many times. Hint: A dictionary
might be useful. Have a go and if you get stuck show us what you've
done and we can help.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos





More information about the Tutor mailing list