qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Qemu-devel] tests/Makefile.include non-portable use of $RANDOM


From: Peter Maydell
Subject: Re: [Qemu-devel] tests/Makefile.include non-portable use of $RANDOM
Date: Thu, 13 Jul 2017 22:15:19 +0100

On 13 July 2017 at 21:16, Eric Blake <address@hidden> wrote:
> On 07/13/2017 12:32 PM, Peter Maydell wrote:
>> On NetBSD the shell complains:
>> # echo $((RANDOM % 255 + 1))
>> -sh: arith: syntax error: "RANDOM % 255 + 1"
>>
>> and so 'make check' doesn't work.
>>
>> Any suggestions for something more portable?
>
> Yes; echo $(( ${RANDOM:-0} % 255 + 1))
>
> should reliably give you 1, even on current BSD /bin/sh, regardless of
> whether BSD adds $RANDOM support in the future.
>
>> https://unix.stackexchange.com/questions/140750/generate-random-numbers-in-specific-range
>> suggests
>>   awk 'BEGIN{srand(); print int(1+rand()*256)}'
>> and of course there are similar things possible with perl et al.
>
> But why bother. MALLOC_PERTURB_ only makes sense on glibc systems, so
> picking a non-random value is no worse on non-glibc systems.

"has glibc" != "has bash as /bin/sh". In particular most
of my build-test systems run ubuntu and will have dash
for /bin/sh.

On the other hand the :-0 trick is simple and probably
good enough.

thanks
-- PMM



reply via email to

[Prev in Thread] Current Thread [Next in Thread]