View Full Version : NewB - How to Generate Random Number
plinydogg
10-21-2006, 09:11 PM
Hello everyone,
Can anyone explain how to generate a random number? I glanced at the help file on the subject but am still confused. I am familiar with the concept of seeding a random number generator but still can't figure out how to generate a random number.
Any help will be much apprecited.
Thanks!
MagNet
10-21-2006, 10:59 PM
random(Int) (The parameter sets the maximum number, it actually won't get generated but zero is included. If you want to make it generate the max number and no zero there's nothing easier than adding "+1" after it.
I hope you know what Int means.
plinydogg
10-22-2006, 05:48 PM
MagNet,
Thanks for your reply. I do know what Int means and I think I understand what you're saying but I still don't know how to create a random variable.
This doesn't work:
numVariable$ = random(9) + 1;
I also need to do something else with rand, srand, etc. don't I?
PointOfLight
10-22-2006, 07:08 PM
When you say your code doesn't work, what's happening? You should use srand(some_number) first to initialize the random number generator, but I think random should work anyway). Here's some sample code, including results (note, since it's "random" your results might vary):
#include "console.ppl"
func WinMain()
InitConsole;
writeln("Random number test");
for(i$, 1, 20)
write(random(1000));
if(i$ < 20)
write(",");
end;
end;
writeln("");
srand(100);
for(i$, 1, 20)
write(random(1000));
if(i$ < 20)
write(",");
end;
end;
return(true);
end;
//results
//665,425,9,774,99,246,998,984,338,714,641,100,287,6 83,172,541,827,584,565,73
//11,37,165,509,747,343,753,51,708,171,727,991,597,5 03,351,397,114,31,678,239
plinydogg
10-23-2006, 12:15 AM
POL: Thank you for your help. I think I misspoke earlier. My code now works in the sense that when I write
someNumber$ = random(1000);
I get a pseudorandom number. But then I'm confused about why you need the other similar functions: rand() and srand().
In my previous [very limited] experience, I've had to seed the number generator before any random number would be generated. But in PPL, it looks like you don't need to seed the generator?
kornalius
10-23-2006, 01:49 AM
rand() is the counterpart of C. It is there for compatibility but also offers a way to make your own random number generator. All you need is the random() function nothing more really.
plinydogg
10-24-2006, 12:51 PM
Thanks for clarifying Kornalius. So, if I wanted to make my own random number generator I would just need to use srand() and rand() exactly as they are used in C? Is there any good reason to do this in PPL or is random() good enough?
PointOfLight
10-24-2006, 02:02 PM
Actually, there's a big difference between rand() and random(num). If you don't care about the maximum value generated, rand works just fine. However, if you want to have a range (say you want a random number between 1 and 1000), then you need to use random, which lets you specify an upper limit to the numbers being generated.
plinydogg
10-24-2006, 03:53 PM
Okay, got it. So, finally (and then I promise I'll be quiet), what does srand do? You don't need it for random(); do you need it for rand()? I'm going to shut up now =)
PointOfLight
10-24-2006, 06:25 PM
I'll have to defer to kornalius on this one. It does not appear that you need srand in order to use rand, so I'm not sure what the purpose of srand is.
kornalius
10-24-2006, 06:51 PM
Initialize random number generator.
Uses seed parameter to set a new starting point for generating random numbers with rand.
If seed is set to 1 the generator is reinitialized to its initial value as before any call to rand or srand.
In order to generate true random numbers it is suggested to use as seed a value that changes often, like the one returned by time function included in <time.h> (the number of seconds elapsed since newyear 1970).
From Google search on srand.
vBulletin® v3.8.4, Copyright ©2000-2012, Jelsoft Enterprises Ltd.