Jump to page: 1 2 3
Thread overview
get random number
Nov 11, 2005
somebody
Nov 11, 2005
Niko Korhonen
Nov 11, 2005
Georg Wrede
Nov 11, 2005
James Dunne
Nov 11, 2005
BCS
Nov 12, 2005
Manfred Nowak
Nov 11, 2005
Manfred Nowak
Nov 12, 2005
BCS
Nov 12, 2005
Carlos Santander
Nov 12, 2005
Manfred Nowak
Nov 12, 2005
Bruno Medeiros
Nov 12, 2005
Manfred Nowak
Nov 12, 2005
Carlos Santander
Nov 14, 2005
Bruno Medeiros
Nov 14, 2005
Manfred Nowak
Nov 15, 2005
Bruno Medeiros
Nov 13, 2005
Walter Bright
Nov 14, 2005
Bruno Medeiros
Nov 13, 2005
Walter Bright
Nov 14, 2005
Oskar Linde
Nov 14, 2005
Don Clugston
Nov 15, 2005
Uwe Salomon
Nov 15, 2005
Niko Korhonen
Nov 15, 2005
kris
Nov 15, 2005
Georg Wrede
Nov 15, 2005
Don Clugston
Nov 15, 2005
Georg Wrede
Nov 15, 2005
Kris
November 11, 2005
how i can get random number on interval from 1 to 100?


November 11, 2005
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
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
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
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
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
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
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
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
Carlos Santander wrote:

[...]
> What's the possible suffering?

Its because the underlying distribution changes dramatically ;-)

-manfred

« First   ‹ Prev
1 2 3