473,796 Members | 2,640 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Randomizing specific range

19 New Member
Hello Friends,
I am doing randomization of intergers for specific range.The way i have done is mentioned below.

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2.  
  3. my @id;
  4. my $idx1;
  5. my $idx2;
  6. my $idx3;
  7. my $idx4;
  8.  
  9.  @id = (4..28); 
  10.    print ("ID range is @id \n");
  11.  
  12.    $idx1 = int(rand @id ); 
  13.    print ("idx1 random is $idx1 \n");
  14.    $idx2 = int(rand @id); 
  15.    print ("idx2 random is $idx2 \n");
  16.    $idx3 = int(rand @id); 
  17.    print ("idx3 random is $idx3 \n");
  18.    $idx4 = int(rand @id); 
  19.    print ("idx4 random is $idx4 \n");
Expand|Select|Wrap|Line Numbers
  1. Output:
  2.   ID range is 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 
  3. idx1 random is 10 
  4. idx2 random is 2 
  5. idx3 random is 14 
  6. dx4 random is 13 
Here my problem is i am not getting the random values with in the given range, see the code which i got output for the program, idx2 value is 2, but the range i have mentioned is 4..28.

Please anyone let me know where i did wrong.

Thanks
Raghavendra
Nov 13 '08 #1
13 1996
KevinADC
4,059 Recognized Expert Specialist
Its because you are using the array @id in scalar context which is the length of the array, not the elements in the array. The array is length 15 so the values returned by your code will be 0 thru 14. What you want to do is use the length of the array to generate a random number to use as an index number of the array which will return the corresponing values in the array. Easier explained with code than words:

Expand|Select|Wrap|Line Numbers
  1.  @id = (4..28); 
  2.    print ("ID range is @id \n");
  3.  
  4.    $idx1 = int($id[rand @id] ); 
  5.    print ("idx1 random is $idx1 \n");
  6.    $idx2 = int($id[rand @id]); 
  7.    print ("idx2 random is $idx2 \n");
  8.    $idx3 = int($id[rand @id]); 
  9.    print ("idx3 random is $idx3 \n");
  10.    $idx4 = int($id[rand @id]); 
  11.    print ("idx4 random is $idx4 \n");
  12.  
Edit:

it should really be written like this:

Expand|Select|Wrap|Line Numbers
  1. $idx1 = $id[int rand @id];
although using 'int' isn't really necessary
Nov 13 '08 #2
eWish
971 Recognized Expert Contributor
I like Kevin's code. However, this will produce a random number between 4 and 28. Just for an example I am printing it 100 times.

Expand|Select|Wrap|Line Numbers
  1. for (1..100) {
  2.     print 4 + int rand( 28-4+1 ), "\n";
  3. }
--Kevin
Nov 13 '08 #3
raghavendrap
19 New Member
Thank you kelvin,
In that code, if i want to generate random values for idx1,idx2,idx3. ...so on..such that the random value generated by idx1 not equal to anyone of the indexes i.e unique and the random value generated by idx2 not equal to anyone of the indexes ..so on i.e
the random values must be unique i.e idx1 != idx2 != idx3 ..so on.....
if i run 100 or 1000 times.....the values must not be same for the range (4..30).


Thanks
Raghavendra
Nov 13 '08 #4
nithinpes
410 Recognized Expert Contributor
Thank you kelvin,
In that code, if i want to generate random values for idx1,idx2,idx3. ...so on..such that the random value generated by idx1 not equal to anyone of the indexes i.e unique and the random value generated by idx2 not equal to anyone of the indexes ..so on i.e
the random values must be unique i.e idx1 != idx2 != idx3 ..so on.....
if i run 100 or 1000 times.....the values must not be same for the range (4..30).


Thanks
Raghavendra
That is an incorrect expectation, logically. You can't expect 100 random numbers generated in the range (4..30) to be unique as the range itself consists of only 26 numbers!
Nov 13 '08 #5
nithinpes
410 Recognized Expert Contributor
However, I will consider a different range(4..108). If you want to generate 100 unique random numbers in this range, use:
Expand|Select|Wrap|Line Numbers
  1. my %unique;
  2. for (1..100) { 
  3.     my $rand =4 + int rand( 108-4+1 ); 
  4.     redo if(exists $unique{$rand});
  5.     print "$rand\t";
  6.     $unique{$rand}++;
  7.  
Nov 13 '08 #6
raghavendrap
19 New Member
Hello nithinpes,
Thank you very much,
My actual scenario is, i have registers from "Ridxinitial... Ridxend"
lets say idxinitial = , idxend =30 i.e i have registers from R4..R30.
tat's y i have taken range (4..30)

i.e @id = (4..30);

Expand|Select|Wrap|Line Numbers
  1. use strict                    
  2. my @id ;
  3. my $idx1;
  4. my $idx2;
  5. my $idx3;
  6. my $idx4;
  7. my $idx5;
  8. my $idx6;
  9. my $idx7;
  10. my $idx8;
  11. my $idx9;
  12. my $idx10;
  13.  
  14.  @id = (4..30);
  15.   $idx1 = $id(int rand @id);
  16.   $idx2 = $id(int rand @id);
  17.   $idx3 = $id(int rand @id);
  18.   $idx4 = $id(int rand @id);
  19.   $idx5 = $id(int rand @id);
  20.   $idx6 = $id(int rand @id);
  21.   $idx7 = $id(int rand @id);
  22.   $idx8 = $id(int rand @id);
  23.   $idx9 = $id(int rand @id);
  24.   $idx10 = $id(int rand @id);
  25.   print("IDX values are  $dx1,$idx2,$idx3,$idx4,$idx5,$idx6,$idx7,$idx8,$idx9,$idx10");
  26.  
If i run the exact code once which i have mentioned above i am getting the output as anyone of the $id values are same,
But what my requirement is if run the code once, the random values generated by & assigned to $idx much be unique.


Thanks & Regards
Raghavendra
Nov 13 '08 #7
nithinpes
410 Recognized Expert Contributor
Hello nithinpes,
Thank you very much,
My actual scenario is, i have registers from "Ridxinitial... Ridxend"
lets say idxinitial = , idxend =30 i.e i have registers from R4..R30.
tat's y i have taken range (4..30)

i.e @id = (4..30);

use strict
my @id ;
my $idx1;
my $idx2;
my $idx3;
my $idx4;
my $idx5;
my $idx6;
my $idx7;
my $idx8;
my $idx9;
my $idx10;

@id = (4..30);
$idx1 = $id(int rand @id);
$idx2 = $id(int rand @id);
$idx3 = $id(int rand @id);
$idx4 = $id(int rand @id);
$idx5 = $id(int rand @id);
$idx6 = $id(int rand @id);
$idx7 = $id(int rand @id);
$idx8 = $id(int rand @id);
$idx9 = $id(int rand @id);
$idx10 = $id(int rand @id);
print("IDX values are $dx1,$idx2,$idx 3,$idx4,$idx5,$ idx6,$idx7,$idx 8,$idx9,$idx10" );

If i run the exact code once which i have mentioned above i am getting the output as anyone of the $id values are same,
But what my requirement is if run the code once, the random values generated by & assigned to $idx much be unique.


Thanks & Regards
Raghavendra
Try the code that I mentioned in my previous post.
Nov 13 '08 #8
raghavendrap
19 New Member
Can u please be clear, i tried alot for that but it's not coming.
Nov 13 '08 #9
nithinpes
410 Recognized Expert Contributor
Can u please be clear, i tried alot for that but it's not coming.
Expand|Select|Wrap|Line Numbers
  1. my %unique;
  2. for (1..10) { 
  3.    my $rand =4 + int rand( 30-4+1 ); 
  4.     redo if(exists $unique{$rand});
  5.     push @rand,$rand;
  6.     $unique{$rand}++;
  7. print "IDX values are:\n" ;
  8. $,="\t";
  9. print @rand;
  10.  
Also, the code you tried will not result in anything. Concentrate on Kevin's code.
Expand|Select|Wrap|Line Numbers
  1. $idx1 = $id(int rand @id);  # should be $idx1 = int($id[rand @id] );
  2. ## OR $idx2 = $id[int rand @id];
  3.  
Nov 13 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

2
3999
by: Susanna | last post by:
Hi all, I'm using the following slideshow script that I found on the web to display changing images with a crossfade effect. Eventually I will be adding many more images to the slideshow. The thing is, I ALSO have to make it so the images will load randomly. I have looked at a number of scripts for random-loading slideshows, but I have to find a way to COMBINE this fading-image script (or, a different fading-image script, if...
3
3642
by: Franky | last post by:
I need to specify a range of outgoing port numbers (i.e., 20000-21000). Is there any way to bind this range so that when system picks up a free port, it will only pick one from this range? I know I can bind a specific port number but I'd rather let the system pick up an available one from the specific range. Can someone tell me how to do that? Thanks in advance,
5
5950
by: sameer_deshpande | last post by:
Hi, I need to create a partition table but the column on which I need to create a partition may not have any logical ranges. So while creating or defining partition function I can not use any range. like CREATE PARTITION FUNCTION my_part_func (NUMERIC(7)) AS RANGE LEFT FOR VALUES (1,100,1000);
1
2288
by: aquarius4u | last post by:
HI, Please let me know how to pull out records for a specific period... like from 2nd Feb 08 to 23rd june '08. I even heard that there is a calendar active x control option that we can use. I have a simple database that store date of births and wants to know how to pull out a specific range of data, using a comand button. what would be the coding like..
3
3416
by: jamieharrop | last post by:
Afternoon all, I've been battling with this all day today and my brain is now pretty much fried. I have one table that lists several details about my customers (name, address, phone, date of birth etc). I want to create a query that will allow me to enter a start date and an end date and will then display the customers who have birthdays within those two dates. I know I can do this using:
9
1666
by: garyb2008 | last post by:
Hello Can someone help me with this one please! Im working on a service level tracking database which records the times that a service goes down during the day (DowntimeStart/DowntimeEnd) and compares this to the times that it is required to be available (SLAStart/SLAEnd). I want to return the amount of time the service was down during the SLA period in minutes. Date, DowntimeStart, DowntimeEnd, SLAStart, SLAEnd 1/1/08, 15:00, ...
5
2790
by: jazznojive | last post by:
I've created a textbox control on a userform and I am trying to figure out how to make the text populate to a worksheet range in sequence automatically. I've figued out how to get the text that the user types in to populate in a specific range, but I'd like each entry from the user to populate on the next cell below the previous entry automatically. I've used .showdataform to do this, but I want the userform to be customized rather than using...
1
1751
by: shahnawazatiq | last post by:
sirs, can anybody give me the VB code for how to copy user specific data from one workbook to other workbook . example:i am having database on one workbook say "data1" and one other workbook say "search_sheet" which is available to user is having a search box and a command button to search it.user will write some letter in search box and on pressing command button data should be retrieved from data1 workbook if it is found else it should...
11
45749
by: CarrieR | last post by:
Hi, I thought this was a simple issue, but apparently it's not. I need to export the contents of about 30 queries, each into a specific sheet, and cell range, of an existing Excel workbook. Here are all of the details I need in one sample transfer: Query = "Query A" Excel workbook = "C:\Documents and Settings\All Users\Workbook1.xlsx" Sheet = "BB DATA" Range = B2:J32
0
10452
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10221
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10169
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10003
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9050
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7546
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5569
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4115
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.