Thread overview | ||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
November 11, 2005 get random number | ||||
---|---|---|---|---|
| ||||
how i can get random number on interval from 1 to 100? |
November 11, 2005 Re: get random number | ||||
---|---|---|---|---|
| ||||
Posted in reply to somebody | somebody wrote: > how i can get random number on interval from 1 to 100? This belongs to the D.learn newsgroup. However, the answer is that you can use the rand() function from package std.random. See http://www.digitalmars.com/d/phobos/std_random.html Here is some example code: import std.random; void main() { // Range 1-100 uint r = cast(uint)((rand() / cast(double)uint.max) * 99.0) + 1; } Modulo arithmetics would be simpler (uint r = rand() % 99 + 1), but the randomness aspect might suffer from that. -- Niko Korhonen SW Developer |
November 11, 2005 Re: get random number | ||||
---|---|---|---|---|
| ||||
Posted in reply to Niko Korhonen | Niko Korhonen wrote:
> somebody wrote:
>
>> how i can get random number on interval from 1 to 100?
>
>
> This belongs to the D.learn newsgroup.
>
> However, the answer is that you can use the rand() function from package std.random. See http://www.digitalmars.com/d/phobos/std_random.html
>
> Here is some example code:
>
> import std.random;
>
> void main()
> {
> // Range 1-100
> uint r = cast(uint)((rand() / cast(double)uint.max) * 99.0) + 1;
> }
>
> Modulo arithmetics would be simpler (uint r = rand() % 99 + 1), but the randomness aspect might suffer from that.
If this is the correct way, then this ought to be in Phobos!
|
November 11, 2005 Re: get random number | ||||
---|---|---|---|---|
| ||||
Posted in reply to Georg Wrede | Georg Wrede wrote:
> Niko Korhonen wrote:
>
>> somebody wrote:
>>
>>> how i can get random number on interval from 1 to 100?
>>
>>
>>
>> This belongs to the D.learn newsgroup.
>>
>> However, the answer is that you can use the rand() function from package std.random. See http://www.digitalmars.com/d/phobos/std_random.html
>>
>> Here is some example code:
>>
>> import std.random;
>>
>> void main()
>> {
>> // Range 1-100
>> uint r = cast(uint)((rand() / cast(double)uint.max) * 99.0) + 1;
>> }
>>
>> Modulo arithmetics would be simpler (uint r = rand() % 99 + 1), but the randomness aspect might suffer from that.
>
>
> If this is the correct way, then this ought to be in Phobos!
I think we should also go with an instantiable Randomizer class. It would not be a replacement for rand(), but a welcome addition. Pseudo-random number generators are powerful things, but are quite limiting when there is only one global number generator allowed.
|
November 11, 2005 Re: get random number | ||||
---|---|---|---|---|
| ||||
Posted in reply to James Dunne | In article <dl2of0$10km$1@digitaldaemon.com>, James Dunne says... > Niko Korhonen wrote: > >> somebody wrote: >> >> >> Here is some example code: >> >> import std.random; >> >> void main() >> { >> // Range 1-100 >> uint r = cast(uint)((rand() / cast(double)uint.max) * 99.0) + 1; >> } > > > > If this is the correct way, then this ought to be in Phobos! how about a template?? import std.stdio; import std.random; template randRange(int min, int max) { int randRange(){return cast(int)((rand()/cast(double)uint.max)*(max-min)+min);} } void main() { int i; for(i=0; i<5; i++)writef("[1,10]\t", randRange!(1,10)(), \n); for(i=0; i<5; i++)writef("[-30,0]\t",randRange!(-30,0)(), \n); for(i=0; i<5; i++)writef("[8,10]\t", randRange!(8,10)(), \n); for(i=0; i<5; i++)writef("[0,15]\t", randRange!(0,15)(), \n); } |
November 11, 2005 Re: get random number | ||||
---|---|---|---|---|
| ||||
Posted in reply to Niko Korhonen | Niko Korhonen wrote: >> how i can get random number on interval from 1 to 100? [...] > Here is some example code: [...] The question is not specified enough to give any example. It is unknown whether the interval is left- or right-closed and from what underlying distribution the random numbers should be drawn. It is quite naive to assume a uniform distribution over the natural elements contained in the interval cited and that the closed interval is meant. This question looks very much like a question of a schoolchild that does not want to solve its homework by itself. -manfred |
November 12, 2005 Re: get random number | ||||
---|---|---|---|---|
| ||||
Posted in reply to Manfred Nowak | In article <Xns970BEDBA5BC95svv1999hotmailcom@63.105.9.61>, Manfred Nowak says... > >Niko Korhonen wrote: > >>> how i can get random number on interval from 1 to 100? >[...] >> Here is some example code: >[...] > >The question is not specified enough to give any example. > >It is unknown whether the interval is left- or right-closed >and from what underlying distribution the random numbers should be >drawn. > >It is quite naive to assume a uniform distribution over the natural elements contained in the interval cited and that the closed interval is meant. > >This question looks very much like a question of a schoolchild that does not want to solve its homework by itself. > >-manfred Or the answer of someone who has some idea what the $%#@ the person is asking for. We can assume they are referring to ints because otherwise they should have said so. After all that's what most compilers do. As to the distribution, a uniform distribution can be transformed into any distribution you want with a little work. All of the answers that were given are correct and reasonable. |
November 12, 2005 Re: get random number | ||||
---|---|---|---|---|
| ||||
Posted in reply to Niko Korhonen | Niko Korhonen escribió: > > Here is some example code: > > import std.random; > > void main() > { > // Range 1-100 > uint r = cast(uint)((rand() / cast(double)uint.max) * 99.0) + 1; > } > > Modulo arithmetics would be simpler (uint r = rand() % 99 + 1), but the randomness aspect might suffer from that. > I'm not familiar with what you just said, so I have to ask: what are you talking about? What's the possible suffering? -- Carlos Santander Bernal |
November 12, 2005 Re: get random number | ||||
---|---|---|---|---|
| ||||
Posted in reply to Georg Wrede | Georg Wrede wrote:
[...]
> If this is the correct way, then this ought to be in Phobos!
The question is: what is correct here ;-)
-manfred
|
November 12, 2005 Re: get random number | ||||
---|---|---|---|---|
| ||||
Posted in reply to Carlos Santander | Carlos Santander wrote:
[...]
> What's the possible suffering?
Its because the underlying distribution changes dramatically ;-)
-manfred
|
Copyright © 1999-2021 by the D Language Foundation