Jump to content
RTollison

random between a range

Recommended Posts

currently use a loop and random to get a value between a range.

first on was simple enough

value1 := random (250) + 1 (value from 1 thru 250)

second halfway simple

value2 := random(500) + 1; set initial random value

while value2 < 251 do

  value2 := random(500) + 1;

value3 := random(750) + 1;

while value3 < 501 do 

  value3 := random(750) + 1;

value4 := random(1000) + 1;

while value4 < 751 do

   value4 := random(1000) + 1;

 

sometimes it is pretty quick, other times is seems to hang for a bit. < 10 seconds

so that got me to thinking, is there a better way to random values bewteen a range?

Share this post


Link to post

So you want 4 random values, one each in the ranges 1-250, 251-500, 501-750, and 751-1000, right:


Try this:

value1 := random(250) + 1;
value2 := random(250) + 250;
value3 := random(250) + 500;
value4 := random(250) + 750;

Each random number is restricted to a 250 range but adding a number to it puts it into different range brackets.

  • Like 1

Share this post


Link to post

Your code makes no sense.

 

Instead of:

value2 := random(500) + 1;
while value2 < 251 do
  value2 := random(500) + 1;

why don't you just:

// Values from 251 to 500
value2 := 251 + random(250);

 

  • Like 1

Share this post


Link to post

thank you all. yeah i didnt think this one thru very well did I. used to our cobol random command and didn't even go to the point of adding a larger value than 1. 

sorry for such a stupid question...

Share this post


Link to post
31 minutes ago, corneliusdavid said:

So you want 4 random values, one each in the ranges 1-250, 251-500, 501-750, and 751-1000, right:


Try this:


value1 := random(250) + 1;
value2 := random(250) + 250;
value3 := random(250) + 500;
value4 := random(250) + 750;

Each random number is restricted to a 250 range but adding a number to it puts it into different range brackets.

value2, value3, value 4 all have an off by one error. 

  • Like 1

Share this post


Link to post
Just now, David Heffernan said:

value2, value3, value 4 all have an off by one error. 

DOH! Corrected:

value1 := random(250) + 1;
value2 := random(250) + 251;
value3 := random(250) + 501;
value4 := random(250) + 751;

 

Share this post


Link to post

got that implemented, thanks again.

now he asked be something else and if i am  correct, he is asking for overlapping numbers with without duplicating them. i can deal with any duplicates with a while statement but here is basically what is asking for. 

value1 = 1-300

value2 = 100-550

value3 = 250-750

value4 = 500-1000

not sure what he is looking for but he wanted me to let him enter the ranges instead of the hardcoded ranges (which is what he said he wanted but now wants to expand upon for his own purposes).

i asked about any of the overlaps if he wanted to force then higher and he said that the overlapping is fine.

so if we retrieve values like

value1=225

value2=200

value3=299

value4=500 

That would be ok. told him that I had no idea how this is helping. why not throw darts at a board.  but he said now he could put his dart board away.

 

Share this post


Link to post
2 hours ago, RTollison said:

now he asked be something else and if i am  correct, he is asking for overlapping numbers with without duplicating them.

One option would be to create 4 lists and fill them with the possible numbers, and then generate a random index for each list, where you remove each found number from subsequent lists before generating indexes for them. For example:

var
  values1, values2, values3, values4: TList<Integer>;
  value1, value2, value3, value4, index: Integer;

  procedure populate(list: TList<Integer>; startValue, endValue: Integer);
  var
    value: Integer;
  begin
    list.Capacity := endValue - startValue + 1;
    for value := startValue to endValue do
      list.Add(value);
  end;

  procedure checkAndRemove(list: TList<Integer>; startValue, endValue, valueToRemove: Integer);
  begin
    if (valueToRemove >= startValue) and (valueToRemove <= endValue) then
      list.Remove(valueToRemove);
  end;

begin
  values1 := TList<Integer>.Create;
  values2 := TList<Integer>.Create;
  values3 := TList<Integer>.Create;
  values4 := TList<Integer>.Create;

  populate(values1, 1, 300);
  populate(values2, 100, 550);
  populate(values3, 250, 750);
  populate(values4, 500, 1000);

  index := random(values1.Count);
  value1 := values1[index];

  checkAndRemove(values2, 100, 550, value1);
  checkAndRemove(values3, 250, 750, value1);
  checkAndRemove(values4, 500, 1000, value1);

  index := random(values2.Count);
  value2 := values2[index];

  checkAndRemove(values3, 250, 750, value2);
  checkAndRemove(values4, 500, 1000, value2);

  index := random(values3.Count);
  value3 := values3[index];

  checkAndRemove(values4, 500, 1000, value3);

  index := random(values4.Count);
  value4 := values4[index];

  values1.Free;
  values2.Free;
  values3.Free;
  values4.Free;
end;

 

Share this post


Link to post
9 hours ago, corneliusdavid said:

Well isn't that handy? How long has that been around? Found it back to at least 10 Seattle...

RandomRange() has been around since Delphi 6.

Edited by Remy Lebeau

Share this post


Link to post
31 minutes ago, Remy Lebeau said:

since Delphi 6.

And I've probably used it sometime in the past--and forgotten about it. (*head slap*)

Share this post


Link to post

holy crap thank you all again. you really don't know how much i appreciate this. beein fighting a bad sinus/ear infection and am way too tired to be using what little of my brain is functioning. 

thank you , thank you, thank you.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×