Path: chuka.playstation.co.uk!news From: "pal" Newsgroups: scee.yaroze.freetalk.english Subject: Re: rand () Date: 13 Mar 2004 10:18:42 GMT Organization: PlayStation Net Yaroze (SCEE) Lines: 21 Message-ID: <01c408e2$176d80e0$8500a8c0@portable-pal> References: NNTP-Posting-Host: amontsouris-108-1-27-56.w81-49.abo.wanadoo.fr X-Newsreader: Microsoft Internet News 4.70.1155 Nigel Critten wrote in article ... > Does anyone have a rand function that will generate a random number > between two given numbers so rand (-10, 10) would generate a number > somewhere between -10 and 10, the built in rand () returns a number > between 0 - 32767 which doesn't help much. I believe most people, including myself, use it that way: (rand() % 21) - 10 where 21 is (10 - -10) + 1. you might want to check whether the bounds are properly included, depending on what you need. I think this code would give something in [-10, +10]. A generic function may looke like: int my_rand(low, high) { return rand() % (high - low + 1) + low; } Note that % is costly, in case you want to do it a lot.